[英]Setting RowSource in Access with Pass through query
我有一个列表框名称动物,行源设置为以下代码,其中查询“动物”是传递查询。 但是,列表框不会填充任何动物。 注意:如果我运行查询“animal”作为独立传递查询它运行正常,它只是不填充列表框。 这几乎就像点击列表框时查询没有执行一样。
Private Sub animallist_Enter()
Dim Q As QueryDef
Dim DB As Database
' Use for dynamic SQL statement'
Dim strSQL As String
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("animal")
strSQL = "Select distinct(animal) From AnimalDB.Animaltable"
Q.SQL = strSQL
Q.Close
Me.animal.RowSource = strSQL
End Sub
如果我通过ODBC连接到“AnimalDB.Animaltable”并运行以下代码(将查询切换到选择而不是通过),列表框将填充动物。
Private Sub animallist_Enter()
Dim Q As QueryDef
Dim DB As Database
' Use for dynamic SQL statement'
Dim strSQL As String
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("animal")
strSQL = "Select distinct(animal) From [AnimalDB_Animaltable]"
Q.SQL = strSQL
Q.Close
Me.animal.RowSource = strSQL
End Sub
为什么传递查询不会填充列表框?
您正在设置querydef,但不使用它。
Me.animal.RowSource = strSQL
应该
Me.animal.RowSource = "animal"
要么
Me.animal.RowSource = Q.Name
您的第二个代码示例有效,因为Access SELECT SQL可以在Access查询中使用,也可以直接用作行源。
PS Q.Close
应该是Set Q = Nothing
,但实际上也不需要,因为它是一个局部变量,在sub的末尾被销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.