Combo Box in Search Form

reaktorblue

Board Regular
Joined
Aug 8, 2007
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello

I have a combo box on my form that gets values from qryTypes and I would like to be able to use these values as a part of my search form. The values display correctly on the form however, I suspect that the code in the VBA below is wrong for cboSearchType.

All I would like to be able to do is select a value from this drop down and include that as part of the search. So if I select Support in the drop down, I would like for the form to display all records with "Support" as the type.

Could anyone assist me with fixing my below code?

Code:
    Dim strWhere As String
    Dim lngLen As Long
    
    If Not IsNull(Me.txtSearchNetworkID) Then
        strWhere = strWhere & "([NetworkID] Like ""*" & Me.txtSearchNetworkID & "*"") AND "
    End If
    If Not IsNull(Me.txtSearchFirstName) Then
        strWhere = strWhere & "([FirstName] Like ""*" & Me.txtSearchFirstName & "*"") AND "
    End If
    If Not IsNull(Me.txtSearchLastName) Then
        strWhere = strWhere & "([LastName] Like ""*" & Me.txtSearchLastName & "*"") AND "
    End If
    If Not IsNull(Me.cboSearchType) Then
        strWhere = strWhere & "([Type] = " & Me.cboSearchType & ") AND "
    End If

    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then
        MsgBox "No criteria", vbInformation, "Nothing to do."
    Else
        strWhere = Left$(strWhere, lngLen)

        Me.Filter = strWhere
        Me.FilterOn = True
    End If
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This part doesn't look right (in all of them)
"([NetworkID] Like ""*" & Me.txtSearchNetworkID & "*"") AND "

I added extra spaces so you can see the difference more clearly.

"([NetworkID] Like '*' " & Me.txtSearchNetworkID & " '*' ) AND "

Suggest you use temporary debug.print statments to see what you end up with whenever doing this. If it's sql, you can dump the entire statement into a new query to see if it runs. Problems are usually highlighted by the interpreter when they don't run.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,600
Members
449,038
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top