I have a recordset populated from a stored procedure
Set PesquisaTabela = cmd.Execute
If Not PesquisaTabela.EOF Then
txtUS.Text = PesquisaTabela.Fields(0)
end if
How can I use this recordset to populate an fplist / listbox in VB6 ?
Using a recordset with several rows, you will need to loop through the rows to populate the listbox. And instead of a textbox, you ill need to use a Listbox I guess.
One way of doing it is as follows;
Dim iIndex as integer
If Not Recordset Is Nothing Then
For iIndex = 0 To Recordset.Fields.Count - 1
ListBox.Add(Recordset.Fields(iIndex).Column)
Next iIndex
End if
you can also use the While loop on the recordset
If Recordset.RecordCount > 0 Then
Recordset.MoveFirst
While Not Recordset.EOF
...
Recordset.MoveNext
Wend
End if
Related
I am attempting to add columns to an ADO recordset in VB6. I simply want to add 4 columns to the end of the table. It's a table we use constantly but we often delete all the data in it and refill it with the next information we want, basically just using it as a means to temporarily hold data.
I have found that since it is ADO I should be able to use the following:
with rs
.fields.append "column name", "enum dataType"
end with
From reading and experimentation it seems that the recordset has to be closed in order to add the columns.
Here's my code:
rs.Open "MeterReads", DataEnvironment7.cnPTracker, adOpenStatic, adLockOptimistic, adCmdTable
' 2019-11-4 Adding in a section to accomadate for days to depeletion
If gbEnableD2D Then
bExists = False
With rs
For Each fField In rs.Fields
If UCase(fField.Name) = UCase("eddB") Then
bExists = True
Exit For
End If
Next
If bExists = False Then
.Close
.Fields.Append "eddB", adDate
.Fields.Append "eddC", adDate
.Fields.Append "eddM", adDate
.Fields.Append "eddY", adDate
.Open
End If
End With
End If
I would expect there to be additional columns added to my table. However, I can look at the table and see they aren't in there. I can see as I have closed the recordset. That it attempts to append the columns to it. However, I open the recordset back up and those columns disappear from the table. When I say they appear I mean using Microsoft Visual Basic's debug system. It basically has a section where it shows your variables and for objects it shows you the items inside and a bit more info. So I know it attempts it. But I cant seem to get it to retain it. Any thoughts or ideas on were I'm screwing up would be great.
If you want to modify the structure of the table in your database, you can use the Microsoft ADO Ext. 6.0 for DDL and Security library (add it to your Project from the Project menu > References). You can find the table you want to modify by iterating through the Catalog object's Tables collection. First create a Catalog object:
Dim objCatalog As ADOX.Catalog
' Create and Open Catalog
Set objCatalog = New ADOX.Catalog
Set objCatalog.ActiveConnection = DataEnvironment7.cnPTracker
I am assuming DataEnvironment7.cnPTracker is your current ADO Connection object.
Then iterate through Tables:
Dim objTable As ADOX.Table
Dim sTableName As String
sTableName = "Customers"
' Check if Table exists
For Each objTable In objCatalog.Tables
If objTable.Name = sTableName Then
' Table found, return reference
Exit For
End If
Next
Then, once you have the table, you can go through the Columns collection to see if it exists:
Dim objColumn As ADOX.Column
For Each objColumn In objTable.Columns
Finally, if you can't find the column, you can add it:
Set objColumn = New ADOX.Column
With objColumn
.Name = "FieldName"
.DefinedSize = 200
.Type = adVarChar
End With
' Append the new field
objTable.Columns.Append objColumn
One approach would be to add the columns when you open the recordset by modifying how you retrieve the data. Instead of using rs.Open with adCmdTable, use adCmdText with a SELECT statement.
SELECT *, NULL AS eddB, NULL AS eddC, NULL AS eddM, NULL AS eddY FROM MeterReads
with rs
.fields.append "column name", "enum dataType"
end withrs.Open "MeterReads", DataEnvironment7.cnPTracker, adOpenStatic, adLockOptimistic, adCmdTable
' 2019-11-4 Adding in a section to accomadate for days to depeletion
If gbEnableD2D Then
bExists = False
With rs
For Each fField In rs.Fields
If UCase(fField.Name) = UCase("eddB") Then
bExists = True
Exit For
End If
Next
If bExists = False Then
.Close
.Fields.Append "eddB", adDate
.Fields.Append "eddC", adDate
.Fields.Append "eddM", adDate
.Fields.Append "eddY", adDate
.Open
End If
End With
End If
I have a slicer that contains items from a pivot table. My users wants the ability to hide certain slicer items even though these items contains data. Is there a way to hide sliceritems via VBA code?
Why not just filter them out? If this is an OLAP PivotTable, see Pivot Table filter out only 1 option
If this is a "Traditional" PivotTable, then you can use a variation of the code I posted at Pivotfields multiple filter
Here's some amended code:
Sub FilterSlicer_Inverse()
Dim slr As Slicer
Dim sc As SlicerCache
Dim si As SlicerItem
Dim i As Long
Dim vItem As Variant
Dim vSelection As Variant
Dim pt As PivotTable
Set sc = ActiveWorkbook.SlicerCaches("Slicer_test")
vSelection = Array("1", "2") <= List the items you want to hide in here
For Each pt In sc.PivotTables
pt.ManualUpdate = True 'Stops PivotTable from refreshing after each PivotItem is changed
Next pt
With sc
.ClearAllFilters
On Error Resume Next 'In case one of the items isn't found
For Each vItem In vSelection
.SlicerItems(vItem).Selected = False
Next vItem
On Error GoTo 0
End With
For Each pt In sc.PivotTables
pt.ManualUpdate = False
Next pt
End Sub
Note that it doesn't matter if you change the PivotItems or the SlicerItems...you get the same result.
I'm a PHP developer, learning ASP.
I've become very reliant on PHP's useful functions: print_r() and var_dump() to see what an array or object contains.
I don't always know what columns are in a Db Table. So, when a SELECT * From Tbl is queried, and the objRS is populated, would I be able to view what the entire object's contents are?
Is this possible in ASP?
<% `my simple Select statement
Dim strDbConnection
Dim objConn
Dim objRS
Dim strSQL
strDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test.mdb;"
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open(strDbConnection)
strSQL = "SELECT * FROM persons"
Set objRS = objConn.Execute(strSQL)
If objRS.EOF Then
Response.Write("No items found")
Else
Do While Not objRS.EOF
' show all columns I can extract here....
objRS.MoveNext()
Loop
End If
objRS.Close()
Set objRS = Nothing
objConn.Close()
Set objConn = Nothing
%>
-- I would like to see what's coming back at me in the objRS, and then cherry-pick the columns after I know what I have access to.
Is there something similar to what I'm used to?
ie: print_r()
You can use objRS.fileds(j).name and objRS.fields(j).value to get the name of columns and values.
For example:
[...]
for j = 0 to objRS.fields.count - 1
response.write(objRS.fields(j).name & " = " & objRS.fields(j).value)
next
lets call it modUser.
In this modUser I have a ADODB record set
Now from this modUser, I would like to open a form. Lets call it frmUser2.
when this frmUser2 initialize. I would like to use the recordset that I already have from modUser. How do I pass this recordset from modUser to frmUser2?
I tried creating a public sub under frmUser2. But I get an error that says "Run time error 13 type mismatch"
here is a snippet
sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount > 1 Then
frmUser2.PopulateList(rs)
End if
in frmUser2 I have the public function ( i tried sub too)
Public Function PopulateList(rs As ADODB.Recordset)
For Count = 0 To rs.RecordCount - 1
LstModels.AddItem rs(0)
rs.MoveNext
Next
rs.close
End Function
I tried show , and I can get the form to appear, but I have no way to pass the record set.
frmUser2.Show
Please help. Thank you
I'm not a fan of how you're trying to do this, but working with what you want to do, first create a public Recordset property in your form and assign the recordset to it from your module before showing the form.
Module code:
Dim objForm As frmUser2
sSQL = "select name from employee"
rs.Open sSQL, ADOCon, adOpenKeyset
If rs.RecordCount.EOF = False Then
Set objForm = New frmUser2
frmUser2.Recordset = rs
frmUser2.Show
End if
Form Code:
Private Sub Form_Load()
If Not Recordset Is Nothing Then
PopulateList
End If
End Sub
Public Function PopulateList()
Recordset.MoveFirst 'defensive, make sure we're on the first record
LstModels.Clear
Do While Recordset.EOF = False
LstModels.AddItem Recordset(0)
Recordset.MoveNext
Next
Recordset.Close
End Function
I think it would be preferable for the module to have a public method that returns an employee recordset. Your form would call that method when it needs the data. Set rsEmployees = modUser.GetEmployees()
I'm trying to populate a series of textboxes with random value from a column.
i get the first textbox filled then it returns run time error 3021 - no current record.
i checked the values and the record I'm trying to retrieve doesn't exceed recordcount for the table.
Debug colours rs.move randomrecord.
Dim rs As DAO.Recordset
Dim recordCount As Long
Dim randomRecord As Long
Set rs = CurrentDb.OpenRecordset("SELECT * FROM besede")
rs.MoveLast
rs.MoveFirst
recordCount = rs.recordCount - 1
MsgBox recordCount
Randomize
Dim i As Integer
For i = 1 To 10
randomRecord = Int((recordCount) * Rnd)
rs.Move randomRecord
Controls("t" & i).SetFocus
Controls("t" & i) = rs!test
Next
You are moving the cursor from the current position, so eventually you are trying to read a record at the end of the recordset. Use
rs.MoveFirst before rs.Move randomRecord
to move from the beginning of the recordset.
Check Office Dev Center for more background information on Recordset.Move.