【发布时间】:2020-10-02 18:31:51
【问题描述】:
我已在 Access 中制作了此表单,我希望完成以下任务。
这里的列表框包含两列,可以多选。我想使用第二列(右列)的值并将它们传递到我为下面的“test2”按钮设置的查询中。
这是我的按钮点击事件的 VBA 代码。
Private Sub test2_Click()
Dim db As dao.Database
Dim qdef As dao.QueryDef
Dim strSQL As String
Set db = CurrentDb
'Build the IN string by looping through the listbox
For i = 0 To Select_Counties2.ListCount - 1
If Select_Counties2.Selected(i) Then
strIN = strIN & "'" & Select_Counties2.Column(1, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE County_GEOID in " & "(" & Left(strIN, Len(strIN) - 1) & ")"
strSQL = strSQL & strWhere
Set qdef = db.CreateQueryDef("User query results", strSQL)
qdef.Close
Set qdef = Nothing
Set db = Nothing
DoCmd.OpenQuery "User query results", acViewNormal
End Sub
谁能告诉我我在代码中做错了什么?谢谢!
【问题讨论】:
-
你只有 WHERE 而没有查询类型或数据源
-
strSQL的值是多少?我看不到你在哪里设置的。我只看到Where子句。 -
一点点调试工作应该会发现逻辑缺陷。逐步调试您的代码。使用 Debug.Print 语句查看变量的值,尤其是使用代码构建的长 SQL 语句。使用 Watches 窗口。
-
是的,您可以添加
Debug.print ("my SQL request : " & strSQL) -
我的错。我错过了选择语句。我刚刚添加了它。
strSQL = "SELECT * FROM age但查询没有返回任何内容