InsaneProgrammer
Programmer
I am trying to populate a list box with records from a database but am having some trouble. There are 4 fields that I would like to put into the list box (lstProducts). In the code below am am able to put in the data from the field ProductNumber but I don't know how to include the other fields (Description, Price, QtyOnHand). Can anyone help?
'Populate the list box with the recordset
If Not MyRS.EOF Then MyRS.MoveFirst
Do Until MyRS.EOF
lstProducts.AddItem MyRS("PartNumber"
MyRS.MoveNext
Loop
Code:
Private Sub Form_Load()
Dim MyCN As New ADODB.Connection
Dim MyRS As New ADODB.Recordset
Dim strConnection As String
Dim strSQL As String
On Error GoTo Load_Error
'Define the Connection
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
strConnection = strConnection & App.Path & "\Pos.mdb;"
strConnection = strConnection & "Persist Security Info=False"
'Open the connection
With MyCN
.ConnectionString = strConnection
.Open
End With
'Define the Query
strSQL = "Select PartNumber,Description,Price,QtyOnHand "
strSQL = strSQL & "From tblParts Order By PartNumber"
'Run the Query
Set MyRS = MyCN.Execute(strSQL)
'Populate the list box with the recordset
If Not MyRS.EOF Then MyRS.MoveFirst
Do Until MyRS.EOF
lstProducts.AddItem MyRS("PartNumber"
MyRS.MoveNext
Loop
Code:
MyRS.Close 'Close the recordset
MyCN.Close 'Close the connection
Exit Sub
Load_Error:
'Message box with Error Number and Description
Beep
MsgBox Err.Number & vbCrLf & Err.Description
MyRS.Close 'Close the recordset
MyCN.Close 'Close the connection
End Sub