我想编写一个非常简单的查询并将其绑定到带有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
但它也不起作用
答案 0 :(得分: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
注意设置在最后一行。