Access 中的 Vb SQL 查询不起作用

Access 中的 Vb SQL 查询不起作用

我正在尝试使用下拉菜单填充给定选择下的项目列表。到目前为止,我能够显示整个表格:

此处下拉列表

然而,我很难适应“WHERE”语句,该语句将使用下拉列表中选择的组来解析查询:

尝试添加“WHERE”时出现空白列表

这是有效的代码:

Private Sub catBox_Change()
Dim SQL As String
SQL = "SELECT CatQuery.[Tool Name], CatQuery.Category " _
& "FROM CatQuery;"
Me.toolBox.RowSource = SQL
Me.toolBox.Requery
End Sub

这是不起作用的代码:

Private Sub catBox_Change()
Dim SQL As String
SQL = "SELECT CatQuery.[Tool Name], CatQuery.Category " _
& "FROM CatQuery" _
& "WHERE CatQuery.Category ='" & Me.toolBox.Column(0) & "';"
Me.toolBox.RowSource = SQL
Me.toolBox.Requery
End Sub

答案1

分配 SQL 字符串后,您是否检查过它?

你省略了FROM CatQuery和之间的空格WHERE...

尝试:

SQL = "SELECT CatQuery.[Tool Name], CatQuery.Category" & vbNewLine & _
      "FROM CatQuery" & vbNewLine & _
      "WHERE CatQuery.Category = '" & Me.toolBox.Column(0) & "';"

答案2

catbox_change调用事件时,toolbox没有焦点,因此没有值。是否可以从剪切框分配值?:

Private Sub catBox_Change()
Dim SQL As String
SQL = "SELECT CatQuery.[Tool Name], CatQuery.Category " _
& "FROM CatQuery" _
& "WHERE CatQuery.Category ='" & Me.catBox.column(x) & "';"
Me.toolBox.RowSource = SQL
Me.toolBox.Requery
End Sub

如果没有,那么您需要将其RowSource分配到另一个下拉列表toolBoxResult

Private Sub catBox_Change()
Dim SQL As String
SQL = "SELECT CatQuery.[Tool Name], CatQuery.Category " _
& "FROM CatQuery" _
& "WHERE CatQuery.Category ='" & Me.toolBox.Column(0) & "';"
Me.toolBoxResult.RowSource = SQL
Me.toolBoxResult.Requery
End Sub

相关内容