-
Say I have a database named test.mdb.
Inside that database exists a table named Includes which houses only two fields.
Now, I want to import all of the contents from one of the fields to a list box. To do that I've created something that looks like this:
Private Sub Form_Load()
Set DB = OpenDatabase("test.mdb")
For i = 1 To 9
Set RS = DB.OpenRecordset("SELECT * FROM _
Includes WHERE ID = " & i & "", dbOpenDynaset)
List1.AddItem "" & RS!IncludeTitle, List1.ListCount
Next i
RS.Close
End Sub
But this seems to only import the first nine (for i = 1 to 9), I know there's an easier way to do it.
Any suggestions? =).
-
try this.
Set dbs = OpenDatabase("test.mdb")
Set rst = dbs.OpenRecordset("select * from include")
While rst.EOF = False
List1.AddItem rst.Fields("NameOfField").Value
rst.MoveNext
Wend
rst.close
dbs.close
-
<?>
Code:
Private Sub Command1_Click()
'Open the database form sub OpenDB
Call OpenDB
'variables for database and recordset
Dim dbs As Database, rst As Recordset
'
'set the database and recordset
Set dbs = Workspaces(0).OpenDatabase(cdbName)
Set rst = dbs.OpenRecordset(ctblName)
'with the recordset do your stuff
With rst
While Not .EOF
List1.AddItem !field1 'list field1
.MoveNext
Wend 'loop
'close database and recordset and set them = nothing
.Close
dbs.Close
End With
Set rst = Nothing
Set dbs = Nothing
End Sub