从表单上的多个组合框控制Access SQL查询

从表单上的多个组合框控制Access SQL查询,sql,ms-access,Sql,Ms Access,在学习了各种半相关的教程之后,我陷入了困境,试图找出如何将表单上的组合框字段发送到查询 我正在使用该查询生成联系人详细信息的子集,从中可以为邮件快照活动创建标签。我希望组合框要么为空,要么结合使用,这样我就可以对选项组合进行筛选。 我已经让我的组合框进行了查找,以检索它们的值,并找到了一个很好的使用IS NULL的教程,但我不理解VBA创建“查看结果”和“清除表单”命令按钮所必需的功能 以下是查询字段条件中引用的我的组合框的名称: [Forms]![SearchForm]![cboStatus]

在学习了各种半相关的教程之后,我陷入了困境,试图找出如何将表单上的组合框字段发送到查询

我正在使用该查询生成联系人详细信息的子集,从中可以为邮件快照活动创建标签。我希望组合框要么为空,要么结合使用,这样我就可以对选项组合进行筛选。 我已经让我的组合框进行了查找,以检索它们的值,并找到了一个很好的使用IS NULL的教程,但我不理解VBA创建“查看结果”和“清除表单”命令按钮所必需的功能

以下是查询字段条件中引用的我的组合框的名称:

[Forms]![SearchForm]![cboStatus]
[Forms]![SearchForm]![cboNewsletter]
这些字段分别称为Status和Description

我正在筛选的查询称为

qryFilter
以下是我的命令按钮的名称:

cmdResults 
cmdClear
cmdResults应该将每个组合框值发送到查询,无论它们是null还是selected,但我无法使其工作,clear应该使组合框为null

我想扩大这一点,包括更多的标准,但我想让它首先工作

感谢您的帮助,提前感谢, 抢劫

编辑: 试图从Patrick处改编此代码:

Private Sub cmdResults_Click()
 Dim tsSql As String
 tsSql = "SELECT * FROM qryAll WHERE "

If cboNewsletter <> "" And Not IsNull(cboNewsletter) Then
 tsSql = tsSql & "qryCorrespondence.NID = " & cboNewsletter & " "
    If (cboStatus <> "" And Not IsNull(cboStatus)) Then
    tsSql = tsSql & " AND "
    End If
 End If

If cboStatus <> "" And Not IsNull(cboStatus) Then
  tsSql = tsSql & "tblCustomers.Status = " & cboStatus & " "
End If

Dim rs As New ADODB.Recordset
rs.Open tsSql, CurrentProject.AccessConnection, 3, 3
End Sub
并在WHERE子句中表示语法错误


有什么建议吗?

在Access中确实没有干净的方法可以做到这一点

假设我有三个组合框,名为:

CMB名称 中巴城市 cmbState

和一个名为:

B工作

现在,如果我想基于按钮单击的组合按钮的内容运行查询,它可以如下所示:

Private Sub btnDoWork_Click()
     Dim tsSql as String
     tsSql = "SELECT * FROM tblUser WHERE "

  If cmbName <> "" and Not ISNull(cmbName) Then
     tsSql = tsSql & "user_name = " & cmbName & " "
    if (cmbCity <> "" and Not IsNull(cmbCity)) or (cmbState <> "" and Not IsNull(cmbState))
         tsSql = tsSql & " AND "
    end if
 End If

 if cmbCity <> "" and not isnull(cmbcity) then
      tsSql = tsSql & "city = " & cmbcity & " "
      if cmbState <> "" and Not IsNull(cmbState) then
           tsSql = tsSql & " AND "
      end if
 end if

if cmbState <> "" and not is null(cmbState) then
      tsSql = tsSql & "state = " & cmbState
end if

MyControl.RowSource = tsSql

End Sub 
  Private Function GetWhere() As String
    Dim strTemp As String

    If Not IsNull(Me!cboStatus) Then
       strTemp = strTemp & " AND tblCustomers.Status = " & Chr(34) & Me!cmbStatus & Chr(34)
    End If
    If Not IsNull(Me!cboNewsletter) Then
       strTemp = strTemp & " AND qryCorrespondence.NID = " & Chr(34) & Me!cboNewsletter & Chr(34)
    End If
    strTemp = Mid(strTemp, 6)
    If Len(strTemp) > 0 Then
       GetWhere = " WHERE " & strTemp
    End If
  End Function

在Access中确实没有干净的方法来实现这一点

假设我有三个组合框,名为:

CMB名称 中巴城市 cmbState

和一个名为:

B工作

现在,如果我想基于按钮单击的组合按钮的内容运行查询,它可以如下所示:

Private Sub btnDoWork_Click()
     Dim tsSql as String
     tsSql = "SELECT * FROM tblUser WHERE "

  If cmbName <> "" and Not ISNull(cmbName) Then
     tsSql = tsSql & "user_name = " & cmbName & " "
    if (cmbCity <> "" and Not IsNull(cmbCity)) or (cmbState <> "" and Not IsNull(cmbState))
         tsSql = tsSql & " AND "
    end if
 End If

 if cmbCity <> "" and not isnull(cmbcity) then
      tsSql = tsSql & "city = " & cmbcity & " "
      if cmbState <> "" and Not IsNull(cmbState) then
           tsSql = tsSql & " AND "
      end if
 end if

if cmbState <> "" and not is null(cmbState) then
      tsSql = tsSql & "state = " & cmbState
end if

MyControl.RowSource = tsSql

End Sub 
  Private Function GetWhere() As String
    Dim strTemp As String

    If Not IsNull(Me!cboStatus) Then
       strTemp = strTemp & " AND tblCustomers.Status = " & Chr(34) & Me!cmbStatus & Chr(34)
    End If
    If Not IsNull(Me!cboNewsletter) Then
       strTemp = strTemp & " AND qryCorrespondence.NID = " & Chr(34) & Me!cboNewsletter & Chr(34)
    End If
    strTemp = Mid(strTemp, 6)
    If Len(strTemp) > 0 Then
       GetWhere = " WHERE " & strTemp
    End If
  End Function

这是按表单查询接口的典型情况。我的做法是在表单的模块中有一个子例程来编写WHERE子句,如下所示:

Private Sub btnDoWork_Click()
     Dim tsSql as String
     tsSql = "SELECT * FROM tblUser WHERE "

  If cmbName <> "" and Not ISNull(cmbName) Then
     tsSql = tsSql & "user_name = " & cmbName & " "
    if (cmbCity <> "" and Not IsNull(cmbCity)) or (cmbState <> "" and Not IsNull(cmbState))
         tsSql = tsSql & " AND "
    end if
 End If

 if cmbCity <> "" and not isnull(cmbcity) then
      tsSql = tsSql & "city = " & cmbcity & " "
      if cmbState <> "" and Not IsNull(cmbState) then
           tsSql = tsSql & " AND "
      end if
 end if

if cmbState <> "" and not is null(cmbState) then
      tsSql = tsSql & "state = " & cmbState
end if

MyControl.RowSource = tsSql

End Sub 
  Private Function GetWhere() As String
    Dim strTemp As String

    If Not IsNull(Me!cboStatus) Then
       strTemp = strTemp & " AND tblCustomers.Status = " & Chr(34) & Me!cmbStatus & Chr(34)
    End If
    If Not IsNull(Me!cboNewsletter) Then
       strTemp = strTemp & " AND qryCorrespondence.NID = " & Chr(34) & Me!cboNewsletter & Chr(34)
    End If
    strTemp = Mid(strTemp, 6)
    If Len(strTemp) > 0 Then
       GetWhere = " WHERE " & strTemp
    End If
  End Function
(上面的代码假设
tblCustomers.Status
qryCorrespondence.NID
qryAll
的SELECT语句中可用)

在cmdResults的OnClick()事件中,您可以这样使用它:

  Dim strSQL As String

  strSQL = "SELECT * FROM qryAll"
  strSQL = strSQL & GetWhere()

  [do with strSQL whatever you want]
如果两个组合框均未选择值,则上述代码将返回所有记录


对于cmbClear,您只需要将两个组合框设置为Null的代码。

这是表单查询接口的典型情况。我的做法是在表单的模块中有一个子例程来编写WHERE子句,如下所示:

Private Sub btnDoWork_Click()
     Dim tsSql as String
     tsSql = "SELECT * FROM tblUser WHERE "

  If cmbName <> "" and Not ISNull(cmbName) Then
     tsSql = tsSql & "user_name = " & cmbName & " "
    if (cmbCity <> "" and Not IsNull(cmbCity)) or (cmbState <> "" and Not IsNull(cmbState))
         tsSql = tsSql & " AND "
    end if
 End If

 if cmbCity <> "" and not isnull(cmbcity) then
      tsSql = tsSql & "city = " & cmbcity & " "
      if cmbState <> "" and Not IsNull(cmbState) then
           tsSql = tsSql & " AND "
      end if
 end if

if cmbState <> "" and not is null(cmbState) then
      tsSql = tsSql & "state = " & cmbState
end if

MyControl.RowSource = tsSql

End Sub 
  Private Function GetWhere() As String
    Dim strTemp As String

    If Not IsNull(Me!cboStatus) Then
       strTemp = strTemp & " AND tblCustomers.Status = " & Chr(34) & Me!cmbStatus & Chr(34)
    End If
    If Not IsNull(Me!cboNewsletter) Then
       strTemp = strTemp & " AND qryCorrespondence.NID = " & Chr(34) & Me!cboNewsletter & Chr(34)
    End If
    strTemp = Mid(strTemp, 6)
    If Len(strTemp) > 0 Then
       GetWhere = " WHERE " & strTemp
    End If
  End Function
(上面的代码假设
tblCustomers.Status
qryCorrespondence.NID
qryAll
的SELECT语句中可用)

在cmdResults的OnClick()事件中,您可以这样使用它:

  Dim strSQL As String

  strSQL = "SELECT * FROM qryAll"
  strSQL = strSQL & GetWhere()

  [do with strSQL whatever you want]
如果两个组合框均未选择值,则上述代码将返回所有记录


对于您的cmbClear,您只需要将两个组合框设置为空的代码。

我找到了一个工作正常的代码
按以下条件键入查询设计器:

[Forms]![我的表格]![myControl]或[Forms]![我的表格]![myControl]为空

如果您的问题是如何使combox为空,则需要使用
comboBox=Null


而不是
comboBox=“
,因为这会给字符串一个空字符串,而
Null
正在彻底清理它。

我找到了一个代码,它起作用了
按以下条件键入查询设计器:

[Forms]![我的表格]![myControl]或[Forms]![我的表格]![myControl]为空

如果您的问题是如何使combox为空,则需要使用
comboBox=Null


而不是
comboBox=“
,因为这给了字符串一个空字符串,而
Null
正在彻底清理它。

如果我想将其更改为仅cmbName和cmbCity,我是否要删除最后的If语句?倒数第二行的3是否需要替换为2?是的,然后必须删除在城市检查中添加and with的if语句。三个代表游标类型和锁定类型,因此无论您计划使用哪种类型来锁定。这里有更多的信息:我不同意“Access中没有干净的方法可以做到这一点”。你只需要动态编写SQL。与许多其他SQL应用程序相比,“动态编写SQL”考虑到其他“企业”应用程序的能力并不完全干净。我认为这很有趣@帕特里克,你似乎考虑访问一个“企业”应用程序(因为你使用比较的“其他”)。我不知道你们可能在谈论什么应用程序,因为据我所知,只有两个类似的应用程序可以与Access相媲美(FileMaker Pro和OpenOffice Base),就我所知,它们也不容易做到这一点。我认为最初的问题使用了令人困惑的术语,也许我只是用不同的方式来解释这个问题。另一方面,您的答案会像我的答案一样动态写入SQL,因此我们的解释似乎是一样的。如果我想将其更改为仅cmbName和cmbCity,是否只删除最后的If语句?倒数第二行的3是否需要替换为2?是的,然后必须删除在城市检查中添加and with的if语句。三个代表游标类型和锁定类型,因此无论您计划使用哪种类型来锁定。这里有更多的信息:我不同意“Access中没有干净的方法可以做到这一点”。你只需要动态编写SQL。与许多其他SQL应用程序相比,“动态编写SQL”考虑到其他“企业”应用程序的能力并不完全干净。我认为这很有趣@帕特里克,你似乎考虑访问