Q: Help displaying recordset
I am trying to open and query an access database with VB 4. I
want to populate 4 different controls with the recordset. two
dbcomboboxes, a dblistbox, and a text box.
What I can't figure out is how to tie the different fields to
the controls. I have found a lot of info on how to do the
query, but not to display the query. Any help would be
appreciated.
Patrick McClard patrick.mcclard@sierra.com
A: Check out the Access section:
Private Sub Command1_Click()
'make a new form with two listboxes and one combobox and a commandbutton
'put this code under the click_event
Dim mydb As Database
Dim sSQL$
Dim rsRecordset As Recordset
Dim counter%
Const dbNAme$ = [yourdatabasenameandlocation]
Const tbName$ = [yourtablename]
list1.Clear
list2.Clear
combo1.Clear
Set mydb = OpenDatabase(dbNAme$)
'select what you want/need
sSQL$ = "SELECT * FROM " & tbName
'fill the recordset with the needed data
Set rsRecordset = mydb.OpenRecordset(sSQL$)
'fill the first listbox with the fieldsnames
For counter% = 0 To rsRecordset.Fields.Count - 1
list1.AddItem rsRecordset.Fields(counter%).Name
Next counter%
'start on top
Do While Not rsRecordset.EOF
'fill second listbox with the first value
list2.AddItem rsRecordset.Fields(0).Value
'fill combobox with the second value
combo1.AddItem rsRecordset.Fields(1).Value
'move to next record
rsRecordset.MoveNext
Loop
rsRecordset.Close
mydb.Close
End Sub
Return