【问题标题】:Setting RowSource in Access with Pass through query使用 Pass through 查询在 Access 中设置 RowSource
【发布时间】:2016-08-05 18:52:55
【问题描述】:

我有一个列表框名称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”并运行以下代码(将查询切换为选择而不是通过),列表框将填充动物。

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

为什么直通查询不会填充列表框?

【问题讨论】:

    标签: sql vba ms-access listbox pass-through


    【解决方案1】:

    您正在设置一个 querydef,但没有使用它。

    Me.animal.RowSource = strSQL
    

    应该是

    Me.animal.RowSource = "animal"
    

    或

    Me.animal.RowSource = Q.Name
    

    您的第二个代码示例有效,因为 Access SELECT SQL 可以在 Access 查询中使用或直接用作行源。

    附: Q.Close 应该是 Set Q = Nothing,但这也不是真正需要的,因为它是一个局部变量,在 sub 结束时被销毁。

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多