忽略一个空的组合框

时间:2014-04-10 19:40:32

标签: vba ms-access combobox ms-access-2007

我有5个组合框,用于过滤列表框。我想这样做,所以不必选择所有5个以使过滤器工作。每个组合框开始空白(“”),我希望能够跳过1或几个并使用我选择的组合进行过滤。以下是其中一个组合框的AfterUpdate代码:

On Error Resume Next

    'These are the comboboxes
   Me.BilletMaterial.RowSource = [MechanicalData.Billet Material]
   Me.BilletNumber.RowSource = [MechanicalData.Billet Number]
   Me.TestType.RowSource = [MechanicalData.Test Type]
   Me.Axis.RowSource = [MechanicalData.Axis]
   Me.Temperature.RowSource = [MechanicalData.Temperature]

   Me.BilletMaterial.Requery    
   Me.BilletNumber.Requery
   Me.TestType.Requery
   Me.Axis.Requery
   Me.Temperature.Requery
'Listbox
Me.DataView.RowSource = "SELECT [Billet Material],[Billet Number], " & _
"[Specimen],[Temperature],[Modulus],[Ultimate Tensile Strength],[Strain]  " & _
"FROM MechanicalData " & _
"WHERE 1=1 " & _
"AND [MechanicalData.Axis] = '" & Me.Axis.Value & "'  " & _
"AND [MechanicalData.Test Type] = '" & TestType.Value & "' " & _
"AND [MechanicalData.Temperature] = " & Temperature.Value & " " & _
"AND If (Not IsNull(BilletNumber.Value)) Then [MechanicalData.Billet Number] = " & BilletNumber.Value & " End if  " & _
"AND [MechanicalData.Billet Material] = '" & BilletMaterial.Value & "' "

我的第一个想法是在SELECT语句的WHERE部分中实现多个“if not not empty ...”语句,但我不确定如何做到这一点,或者即使它是可能的。

我知道这行的语法是错误的。

"AND If (Not IsNull(BilletNumber.Value)) Then [MechanicalData.Billet Number] = " & BilletNumber.Value & " End if  " & _

2 个答案:

答案 0 :(得分:2)

我相信你上面的代码缺少'FROM MechanicalData'子句。使用上面的建议,以下内容应该有效(假设所有组合框值都是非数字的):

Dim strWhere    As String

strWhere = " WHERE (1=1)"
If Not IsNull(Axis.Value) Then strWhere = strWhere & " AND ([MechanicalData.Axis] = '" & Axis.Value & "') "
If Not IsNull(TestType.Value) Then strWhere = strWhere & " AND ([MechanicalData.Test Type] = '" & TestType.Value & "') "
If Not IsNull(Temperature.Value) Then strWhere = strWhere & " AND ([MechanicalData.Temperature] = '" & Temperature.Value & "') "
If Not IsNull(BilletNumber.Value) Then strWhere = strWhere & " AND ([MechanicalData.Billet Number] = '" & BilletNumber.Value & "') "
If Not IsNull(BilletMaterial.Value) Then strWhere = strWhere & " AND ([MechanicalData.Billet Material] = '" & BilletMaterial.Value & "') "

Me.DataView.RowSource = "SELECT [Billet Material],[Billet Number], " & _
        "[Specimen],[Temperature],[Modulus],[Ultimate Tensile Strength],[Strain] " & _
        "From MechanicalData " & _
        strWhere

答案 1 :(得分:0)

这应该有效:

'Listbox
Me.DataView.RowSource = "SELECT [Billet Material],[Billet Number], " & _
"[Specimen],[Temperature],[Modulus],[Ultimate Tensile Strength],[Strain]  " & _
"FROM MechanicalData " & _
"WHERE 1=1 " & _
"AND [MechanicalData.Axis] = '" & Nz(Me.Axis.Value, '*') & "'  " & _
"AND [MechanicalData.Test Type] = '" & Nz(TestType.Value, '*') & "' " & _
"AND [MechanicalData.Temperature] = " & Nz(Temperature.Value, '*') & " " & _
"AND If (Not IsNull(BilletNumber.Value)) Then [MechanicalData.Billet Number] = " & BilletNumber.Value & " End if  " & _
"AND [MechanicalData.Billet Material] = '" & Nz(BilletMaterial.Value, '*') & "' "

您可能需要调整以获得正确的单引号/双引号,但使用

  

Nz(SomeControl.Value," *")

绝对是返回所有值的有效方法。