使用传递查询在 Access 中设置 RowSource
Setting RowSource in Access with Pass through query
我有一个名为 animal 的列表框,行源设置为以下代码,其中查询 "animal" 是传递查询。但是,列表框中没有填充任何动物。注意:如果我 运行 查询 "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" 以下代码(将查询切换到 select 而不是通过),列表框将填充动物。
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 查询中使用或直接用作行源。
P.S。 Q.Close
应该是 Set Q = Nothing
,但这也不是真正需要的,因为它是一个局部变量,在子程序结束时被销毁。
我有一个名为 animal 的列表框,行源设置为以下代码,其中查询 "animal" 是传递查询。但是,列表框中没有填充任何动物。注意:如果我 运行 查询 "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" 以下代码(将查询切换到 select 而不是通过),列表框将填充动物。
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 查询中使用或直接用作行源。
P.S。 Q.Close
应该是 Set Q = Nothing
,但这也不是真正需要的,因为它是一个局部变量,在子程序结束时被销毁。