我已编写此代码以通过VBA代码填充列表框但它无法正常工作。我无法理解它有什么问题。
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
答案 0 :(得分:1)
更改
Me.List0.RowSource = hotels
到
Me.List0.RowSource = strsql
您正在尝试将列表行源设置为表,Access无法理解。它想要一个SQL字符串,“strsql”就是那个字符串。
你也可以直接设置它:
Me.List0.RowSource = "select hotel_id, hotel_name from Hotels"