Article ID: 149254
Article Last Modified on 10/11/2006
Sub DatatoListbox()
Dim db As Database
Dim rs As Recordset
Dim varrecords As Variant
'Open the Microsoft Access database.
'Note: By default, Northwind.mdb is installed in the
'\msoffice\access\samples directory. If you installed Microsoft
'Access to a different location, you will have to modify the path
'in the following line of code to point to the location of
'Northwind.mdb.
Set db = OpenDatabase("C:\msoffice\access\samples\northwind.mdb")
'Retrieve the data from the database.
Set rs = db.OpenRecordset("Select productname from Products
where(products.unitprice>=10.00)")
'Move the pointer to the last record of the retrieved data.
rs.MoveLast
'Count the number of records that are being returned and assign that
'number to a variable.
x = rs.RecordCount
'Move the record pointer to the first record. This is required in
'order to return the number of records specified by the RecordCount
'Property. If this is not done, the record pointer will remain on
'the last record and only the last record will be returned.
rs.MoveFirst
'Return the records to an array variable.
varrecords = rs.GetRows(x)
'Fill the list box.
DialogSheets(1).ListBoxes(1).List = Array(varrecords)
'Display the dialog sheet.
DialogSheets(1).Show
End Sub
OpenDatabase
OpenRecordset
MoveLast
MoveFirst
GetRows
Recordcount
Keywords: kbprogramming KB149254