Rizwan Safdar
Rizwan Safdar

Reputation: 135

what is wrong with this code of listbox in msaccess vba

I have wrote this code to populate the listbox through VBA Code but its not working. i cant understand what's wrong with it.

Private Sub Form_Load()
Dim db As DAO.Database
Dim rs As DAO.Recordset

Dim strsql As String
strsql = "select hotel_id, hotel_name from Hotels"
Set db = CurrentDb
Set rs = db.OpenRecordset(strsql)
Me.List0.RowSource = hotels                            'where hotels is name of table
Me.List0.ColumnWidths = "1 in; 2 in"
End Sub

Upvotes: 0

Views: 152

Answers (1)

Johnny Bones
Johnny Bones

Reputation: 8402

Change

Me.List0.RowSource = hotels 

to

Me.List0.RowSource = strsql

You're trying to set your list rowsource to a table, which Access doesn't understand. it wants a SQL string, and "strsql" is that string.

You could also just set it directly like:

Me.List0.RowSource = "select hotel_id, hotel_name from Hotels"

Upvotes: 1

Related Questions