简体   繁体   中英

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM