1

我想编写一个非常简单的查询并将其绑定到带有 VBA 代码的列表框,但是运行这段代码会发生错误:

Private Sub Command0_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM funk", dbOpenTable)
Me.List9.Recordset = rs
End Sub

数据库的名称是正确的。你能告诉我如何解决这个问题吗?

编辑

我已将代码更改为

Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM funk")
Me.List9.Recordset = rs

但它也不起作用

4

1 回答 1

1

不需要这样的复杂性:

Private Sub Command0_Click()
Me.List9.Rowsource = "SELECT * FROM TableName"
End Sub

但是,最好说:

Me.List9.Rowsource = "SELECT ID,VisibleField FROM TableName"

您可以隐藏 ID 字段,但简单选择中的任何选择都等于绑定列为 1 时的 ID。

你会需要:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
'dbOpenDynaset is the default here, but it would not be the default for 
'a local table
Set rs = db.OpenRecordset("SELECT * FROM funk",dbOpenDynaset)
Set Me.List9.Recordset = rs

注意设置在最后一行。

于 2012-10-01T10:29:19.390 回答