Question

I have 5 combo boxes, used to filter a listbox. I want to make it so not all 5 have to be selected in order for the filter to work. Each combobox starts blank ( "" ), and I want to be able to skip 1 or several and filter with the ones I select. Here is the AfterUpdate code for one of the comboboxes:

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 & "' "

My first thought would be to implement multiple "if is not empty..." statements in the WHERE portion of the SELECT statement, but I'm not sure how to do that or even if it's possible.

I know the syntax of this line is wrong.

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

Solution

I believe the code you have above is missing the 'FROM MechanicalData' clause. Using the suggestions from above, the following should work (assuming all combobox values are non-numeric):

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

OTHER TIPS

This should work:

'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, '*') & "' "

You may have to tweak that to get the right single quote/double quote thing, but using

Nz(SomeControl.Value, "*")

is definitely a valid way to return all values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top