0

我已经编写了这段代码来通过 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
4

1 回答 1

1

改变

Me.List0.RowSource = hotels 

Me.List0.RowSource = strsql

您正在尝试将列表行源设置为 Access 无法理解的表。它需要一个 SQL 字符串,而“strsql”就是那个字符串。

您也可以直接设置它:

Me.List0.RowSource = "select hotel_id, hotel_name from Hotels"
于 2013-09-12T18:37:46.130 回答