Filter with multiple textboxes and Combo's

DimitriPeeters

New member
Local time
Today, 16:38
Joined
Jan 22, 2013
Messages
2
Hi, I'm new here but I hope someone can help me out. I'm a beginner in Access programming and I have the folowing problem:

I have a form with a big List box in it. This box contains certificate numbers, languages, client names, calibration dates, ....

I would like to make a filter on this box with multiple textboxes and Combo boxes. I know how to filter with just one Textbox but when I start to use more than one box, something goes wrong and the result is an empty Listbox.

One textbox contains the client name, and 2 others contain the dates where between the calibration has took place.
There is also one Combo box for the type of certificate and one for the language of the certificate.

Not all the boxes are always used at the same time, but it should be possible.

Can someone help me out because for me this is a little bit difficult due to my beginner state in access programming. My database is created in access 2010 as an .accdb database.
 
Hello DimitriPeeters, Welcome to AWF.. :)

Well your design does not give enough information to tackle the problem, as we are nto sure how this filter acts.. So will be helpful if there is a bit more information.. What have you done so far?

What you might also want; is to look into creating Dynamic Queries..
 
This is possible by building a criteria string. To do this you need to test each text box to see if there is a value, if it's NULL then ignore it otherwise concatenate it to the criteria string.
You just then need to set the rowsource for your listbox using vba and apply the criteria string
David
 
Thanks for the link, I think it could be a part of the solution.

What I've done so far:
I was able to filter when I was just using one textbox. I did it through folowing code:

Textbox On Change:

'code that happens when users types in the textbox
Dim strFullist As String
Dim strFilteredlist As String

If blnSpace = False Then
Me.Refresh 'refresh to make sure the textbox changes are actually available to use

'specify the default/full rowsource for the control

strFullist = "SELECT tbl_certificaatnr.cer_Jaartal AS Jaar, tbl_certificaatnr.cer_Certificaatnummer AS Nummer, tbl_certificaatnr.cer_Certificaattype AS Type, tbl_certificaatnr.cer_Taalcertificaat, tbl_klanten.kla_ID AS [ID Klant], tbl_klanten.kla_Maatschappij AS Maatschappij, tbl_klanten.kla_afdeling AS Afdeling, tbl_TypeToestel.tpt_NaamToestelNL AS [Naam toestel], tbl_Merk.mrk_naam_merk_toestel AS Merk, tbl_certificaatnr.cer_Aanvrager AS Aanvrager, tbl_Prestatie.pre_fait AS Uitgevoerd, tbl_certificaatnr.cer_Kalibratiedatum AS Kalibratiedatum, tbl_certificaatnr.cer_ID AS [ID Cert] "
strFullist = strFullist & "FROM tbl_Offerte RIGHT JOIN (tbl_klanten INNER JOIN (((tbl_Prestatie RIGHT JOIN tbl_certificaatnr ON tbl_Prestatie.pre_ID = tbl_certificaatnr.cer_IDPrestation) LEFT JOIN (tbl_Toes LEFT JOIN tbl_Merk ON tbl_Toes.toe_IDMerk = tbl_Merk.mrk_id) ON tbl_Prestatie.pre_Toestel = tbl_Toes.toe_ID) LEFT JOIN tbl_TypeToestel ON tbl_Toes.toe_IDType = tbl_TypeToestel.tpt_ID) ON tbl_klanten.kla_ID = tbl_certificaatnr.cer_IDKlant) ON tbl_Offerte.off_ID = tbl_Prestatie.pre_IDOfferte "
strFullist = strFullist & "ORDER BY tbl_certificaatnr.cer_Jaartal DESC , tbl_certificaatnr.cer_Certificaatnummer DESC; "

strFilteredlist = "SELECT tbl_certificaatnr.cer_Jaartal AS Jaar, tbl_certificaatnr.cer_Certificaatnummer AS Nummer, tbl_certificaatnr.cer_Certificaattype AS Type, tbl_certificaatnr.cer_Taalcertificaat, tbl_klanten.kla_ID AS [ID Klant], tbl_klanten.kla_Maatschappij AS Maatschappij, tbl_klanten.kla_afdeling AS Afdeling, tbl_TypeToestel.tpt_NaamToestelNL AS [Naam toestel], tbl_Merk.mrk_naam_merk_toestel AS Merk, tbl_certificaatnr.cer_Aanvrager AS Aanvrager, tbl_Prestatie.pre_fait AS Uitgevoerd, tbl_certificaatnr.cer_Kalibratiedatum AS Kalibratiedatum, tbl_certificaatnr.cer_ID AS [ID Cert] "
strFilteredlist = strFilteredlist & "FROM tbl_Offerte RIGHT JOIN (tbl_klanten INNER JOIN (((tbl_Prestatie RIGHT JOIN tbl_certificaatnr ON tbl_Prestatie.pre_ID = tbl_certificaatnr.cer_IDPrestation) LEFT JOIN (tbl_Toes LEFT JOIN tbl_Merk ON tbl_Toes.toe_IDMerk = tbl_Merk.mrk_id) ON tbl_Prestatie.pre_Toestel = tbl_Toes.toe_ID) LEFT JOIN tbl_TypeToestel ON tbl_Toes.toe_IDType = tbl_TypeToestel.tpt_ID) ON tbl_klanten.kla_ID = tbl_certificaatnr.cer_IDKlant) ON tbl_Offerte.off_ID = tbl_Prestatie.pre_IDOfferte "
strFilteredlist = strFilteredlist & "WHERE [kla_Maatschappij] like ""*" & Me.MaatschappijTextbox & "*"" "
strFilteredlist = strFilteredlist & "ORDER BY tbl_certificaatnr.cer_Jaartal DESC , tbl_certificaatnr.cer_Certificaatnummer DESC; "

fLiveSearchMaatschappij Me.MaatschappijTextbox, Me.Liste, strFullist, strFilteredlist
End If


Textbox On Got Focus:

On Error Resume Next

If Me.MaatschappijTextbox.Value = "" Then

Me.MaatschappijTextbox.Value = ""
End If

Textbox On Lost Focus:

Private Sub MaatschappijTextbox_LostFocus()
On Error Resume Next

If Me.MaatschappijTextbox.Value = "" Then
Me.MaatschappijTextbox.Value = ""
End If

End Sub

and a public function:

Function fLiveSearchMaatschappij(MaatschappijTextbox As TextBox, Liste As Control, strFullist As String, strFilteredlist As String)
'==================================================================================
' THIS FUNCTION ALLOWS YOU TO FILTER A COMBO BOX OR LIST BOX AS THE USER TYPES
' ALL YOU NEED TO DO IS PASS IN THE CONTROL REFERENCE TO THE SEARCH BOX ON YOUR
' FORM, THE LISTBOX/COMBO BOX YOU WANT TO FILTER, AND WHAT THE FULL AND FILTERED
' SQL (ROWSOURCE) SHOULD BE.
'
' ctlSearchBox THE TEXTBOX THE USER TYPES IN TO SEARCH
'
' ctlFilter THE LISTBOX OR COMBOBOX ON THE FORM YOU WANT TO FILTER
'
' strFullSQL THE FULL ROWSOURCE YOU WANT TO DISPLAY AS A DEFAULT IF NO
' RESULTS ARE RETURNED
'
' strFilteredSQL THE FILTERED ROWSOURCE FOR THE LISTBOX/COMBOBOX; FOR EXAMPLE
' YOU WOULD WANT TO USE '...like ""*" & me.txtsearch.value & "*"""
' TO FILTER THE RESULTS BASED ON THE USER'S SEARCH INPUT
'
' ctlCountLabel (OPTIONAL) THE LABEL ON YOUR FORM WHERE YOU WANT TO DISPLAY THE
' COUNT OF ROWS DISPLAYED IN THE LISTBOX/COMBOBOX AS THEY SEARCH
'=====================================================================================


'ADVANCED PARAMETERS - Change these constants to change the behaviour of the search
Const iSensitivity = 1 'Set to the number of characters the user must enter before the search starts
Const blnEmptyOnNoMatch = False 'Set to true if you want nothing to appear if nothing matches their search


On Error GoTo err_handle

'restore the cursor to where they left off
MaatschappijTextbox.SetFocus
MaatschappijTextbox.SelStart = Len(MaatschappijTextbox.Value) + 1

If MaatschappijTextbox.Value <> "" Then
'Only fire if they've input more than two characters (otherwise it's wasteful)

If Len(MaatschappijTextbox.Value) > iSensitivity Then
Liste.RowSource = strFilteredlist

If Liste.ListCount > 0 Then
MaatschappijTextbox.SetFocus
MaatschappijTextbox.SelStart = Len(MaatschappijTextbox.Value) + 1

Else
If blnEmptyOnNoMatch = True Then
Liste.RowSource = ""

Else
Liste.RowSource = strFullist
End If
End If

Else
Liste.RowSource = strFullist
End If

Else
Liste.RowSource = strFullist
End If


Exit Function
err_handle:
Select Case Err.Number
Case 91 'no txtcount
'exit
Case 94 'null string
'exit
Case Else
MsgBox "An unexpected error has occurred: " & vbCrLf & Err.Description & _
vbCrLf & "Error " & Err.Number & vbCrLf & "Line: " & Erl
End Select


End Function


I found this code on this forum I think and it works good.

Now I want to extend this code. I want to enter a Start date in one textbox and an End Date in another textbox, this date refers to the calibration date of the equipment.

I also have a Combo box where I can select te type of certificate and another one where I can select the language of the certificate. However, untill now I didn't used the 2 comboboxes because they don't work together with the code above.

I've tried to extend the basic code above like this:

Private Sub MaatschappijTextbox_Change()

'code that happens when users types in the textbox
Dim strFullist As String
Dim strFilteredlist As String

If blnSpace = False Then
Me.Refresh 'refresh to make sure the textbox changes are actually available to use

'specify the default/full rowsource for the control

strFullist = "SELECT tbl_certificaatnr.cer_Jaartal AS Jaar, tbl_certificaatnr.cer_Certificaatnummer AS Nummer, tbl_certificaatnr.cer_Certificaattype AS Type, tbl_certificaatnr.cer_Taalcertificaat, tbl_klanten.kla_ID AS [ID Klant], tbl_klanten.kla_Maatschappij AS Maatschappij, tbl_klanten.kla_afdeling AS Afdeling, tbl_TypeToestel.tpt_NaamToestelNL AS [Naam toestel], tbl_Merk.mrk_naam_merk_toestel AS Merk, tbl_certificaatnr.cer_Aanvrager AS Aanvrager, tbl_Prestatie.pre_fait AS Uitgevoerd, tbl_certificaatnr.cer_Kalibratiedatum AS Kalibratiedatum, tbl_certificaatnr.cer_ID AS [ID Cert] "
strFullist = strFullist & "FROM tbl_Offerte RIGHT JOIN (tbl_klanten INNER JOIN (((tbl_Prestatie RIGHT JOIN tbl_certificaatnr ON tbl_Prestatie.pre_ID = tbl_certificaatnr.cer_IDPrestation) LEFT JOIN (tbl_Toes LEFT JOIN tbl_Merk ON tbl_Toes.toe_IDMerk = tbl_Merk.mrk_id) ON tbl_Prestatie.pre_Toestel = tbl_Toes.toe_ID) LEFT JOIN tbl_TypeToestel ON tbl_Toes.toe_IDType = tbl_TypeToestel.tpt_ID) ON tbl_klanten.kla_ID = tbl_certificaatnr.cer_IDKlant) ON tbl_Offerte.off_ID = tbl_Prestatie.pre_IDOfferte "
strFullist = strFullist & "ORDER BY tbl_certificaatnr.cer_Jaartal DESC , tbl_certificaatnr.cer_Certificaatnummer DESC; "

strFilteredlist = "SELECT tbl_certificaatnr.cer_Jaartal AS Jaar, tbl_certificaatnr.cer_Certificaatnummer AS Nummer, tbl_certificaatnr.cer_Certificaattype AS Type, tbl_certificaatnr.cer_Taalcertificaat, tbl_klanten.kla_ID AS [ID Klant], tbl_klanten.kla_Maatschappij AS Maatschappij, tbl_klanten.kla_afdeling AS Afdeling, tbl_TypeToestel.tpt_NaamToestelNL AS [Naam toestel], tbl_Merk.mrk_naam_merk_toestel AS Merk, tbl_certificaatnr.cer_Aanvrager AS Aanvrager, tbl_Prestatie.pre_fait AS Uitgevoerd, tbl_certificaatnr.cer_Kalibratiedatum AS Kalibratiedatum, tbl_certificaatnr.cer_ID AS [ID Cert] "
strFilteredlist = strFilteredlist & "FROM tbl_Offerte RIGHT JOIN (tbl_klanten INNER JOIN (((tbl_Prestatie RIGHT JOIN tbl_certificaatnr ON tbl_Prestatie.pre_ID = tbl_certificaatnr.cer_IDPrestation) LEFT JOIN (tbl_Toes LEFT JOIN tbl_Merk ON tbl_Toes.toe_IDMerk = tbl_Merk.mrk_id) ON tbl_Prestatie.pre_Toestel = tbl_Toes.toe_ID) LEFT JOIN tbl_TypeToestel ON tbl_Toes.toe_IDType = tbl_TypeToestel.tpt_ID) ON tbl_klanten.kla_ID = tbl_certificaatnr.cer_IDKlant) ON tbl_Offerte.off_ID = tbl_Prestatie.pre_IDOfferte "
strFilteredlist = strFilteredlist & "WHERE [kla_Maatschappij] like ""*" & Me.MaatschappijTextbox & "*"" AND (((tbl_certificaatnr.cer_Kalibratiedatum) > Me.DateBegin And < Me.DateEinde Or (tbl_certificaatnr.cer_Kalibratiedatum) Is Null)) and [cer_Certificaattype] like Me.ComboType & AND [cer_Taalcertificaat] like Me.ComboTaal "
strFilteredlist = strFilteredlist & "ORDER BY tbl_certificaatnr.cer_Jaartal DESC , tbl_certificaatnr.cer_Certificaatnummer DESC; "

fLiveSearchMaatschappij Me.MaatschappijTextbox, Me.Liste, strFullist, strFilteredlist
End If


End Sub



Private Sub MaatschappijTextbox_GotFocus()
On Error Resume Next

If Me.MaatschappijTextbox.Value = "" Then

Me.MaatschappijTextbox.Value = ""
End If

End Sub

I've created the same public folders for all the text boxes and combo boxes and edited the function names for the different textboxes and combo's.

The idea is that my collegue can enter a client, the date where between the calibration has been done, eventually the type of certificate for clients with al lot of material and a language for multilanguage clients. I wouls like to use the textboxes and combo's in combination as well as each one individual.

I know it's maybe difficult to read, the tables are written in Dutch.

Thanks for the help
 
Hi, im new to VBA but I had a similar problem. I have a database for managing items of furniture and created a search form that can filter results by Building, Item and who has reserved the item. Each combo box filters its results based on the one above so if you select a building only available items in that building will be selectable in the items box. I had alot of trouble getting the third combo box to filter based on the two above it so instead I created one combo box for every possible combination of selctions (Combo1 is Null and Combo2 is Null, Combo1 is not null and Combo2 is Null etc) and have them all hidden only to have the one required visable based on the selctions above. As the boxes for the third selction are all on top of eachother the user doesnt even know whats happening.
The VBA for the top two boxes is
Code:
Private Sub Combo10_BeforeUpdate(Cancel As Integer)
On Error GoTo errhandler
    Me.Combo12 = ""
    Me.Combo12.Requery
    Me.Combo14 = ""
    Me.Combo14.Requery
    Me.Combo23 = ""
    Me.Combo23.Requery
    Me.Combo25 = ""
    Me.Combo25.Requery
    Me.Combo27 = ""
    Me.Combo27.Requery
    Dim box1 As String
    Dim box2 As String
    Dim name1 As String
    Dim name2 As String
    Dim name3 As String
    Dim name4 As String
    box1 = Me.Combo10 & ""
    box2 = Me.Combo12 & ""
    name1 = Me.Combo14 & ""
    name2 = Me.Combo23 & ""
    name3 = Me.Combo25 & ""
    name4 = Me.Combo27 & ""
 
    If box1 = "" And box2 = "" Then
    GoTo 1
    ElseIf box1 <> "" And box2 = "" Then
    GoTo 2
    ElseIf box1 <> "" And box2 <> "" Then
    GoTo 3
    ElseIf box1 = "" And box2 <> "" Then
    GoTo 4
    End If
1
 
    Me.Combo14.Visible = False
    Me.Combo23.Visible = False
    Me.Combo25.Visible = False
    Me.Combo27.Visible = True
GoTo 5
2
    Me.Combo23.Visible = True
    Me.Combo27.Visible = False
    Me.Combo14.Visible = False
    Me.Combo25.Visible = False
GoTo 5
3
    Me.Combo23.Visible = False
    Me.Combo14.Visible = True
    Me.Combo27.Visible = False
    Me.Combo25.Visible = False
GoTo 5
4
    Me.Combo14.Visible = False
    Me.Combo23.Visible = False
    Me.Combo25.Visible = True
    Me.Combo27.Visible = False
GoTo 5
5
exit_sub:
    Exit Sub
errhandler:
    MsgBox Err.Description, vbOKOnly, "Error"
    Dim errname As String
 
    errname = Chr(34) & Err.Description & Chr(34)
    sqla = "INSERT INTO [Event log] ([Event])VALUES(" & errname & ") ; "
 
    DoCmd.RunSQL sqla
GoTo exit_sub
End Sub
Private Sub Combo12_BeforeUpdate(Cancel As Integer)
On Error GoTo errhandler
    Dim Building As String
    Dim Item As String
    Dim Name As String
 
    Building = Me.Combo10 & ""
    Item = Me.Combo12 & ""
    Name = Me.Combo14 & ""
 
    If Building <> "" Then
    GoTo 1
    ElseIf Building = "" Then
    GoTo 2
    End If
1
    Me.Combo14 = ""
    Me.Combo14.Requery
    Me.Combo23 = ""
    Me.Combo23.Requery
    Me.Combo25 = ""
    Me.Combo25.Requery
    Me.Combo27 = ""
    Me.Combo27.Requery
GoTo 3
2
    Me.Combo10 = ""
    Me.Combo10.Requery
    Me.Combo14 = ""
    Me.Combo14.Requery
    Me.Combo23 = ""
    Me.Combo23.Requery
    Me.Combo25 = ""
    Me.Combo25.Requery
    Me.Combo27 = ""
    Me.Combo27.Requery
GoTo 3
3
    Dim box1 As String
    Dim box2 As String
    Dim name1 As String
    Dim name2 As String
    Dim name3 As String
    Dim name4 As String
 
    box1 = Me.Combo10 & ""
    box2 = Me.Combo12 & ""
    name1 = Me.Combo14 & ""
    name2 = Me.Combo23 & ""
    name3 = Me.Combo25 & ""
    name4 = Me.Combo27 & ""
 
    If box1 = "" And box2 = "" Then
    GoTo 4
    ElseIf box1 <> "" And box2 = "" Then
    GoTo 5
    ElseIf box1 <> "" And box2 <> "" Then
    GoTo 6
    ElseIf box1 = "" And box2 <> "" Then
    GoTo 7
    End If
4
    Me.Combo14.Visible = False
    Me.Combo23.Visible = False
    Me.Combo25.Visible = False
    Me.Combo27.Visible = True
GoTo 8
5
    Me.Combo23.Visible = True
    Me.Combo27.Visible = False
    Me.Combo14.Visible = False
    Me.Combo25.Visible = False
GoTo 8
6
    Me.Combo23.Visible = False
    Me.Combo14.Visible = True
    Me.Combo27.Visible = False
    Me.Combo25.Visible = False
GoTo 8
7
    Me.Combo14.Visible = False
    Me.Combo23.Visible = False
    Me.Combo25.Visible = True
    Me.Combo27.Visible = False
GoTo 8
8
 
exit_sub:
    Exit Sub
errhandler:
    MsgBox Err.Description, vbOKOnly, "Error"
    Dim errname As String
 
    errname = Chr(34) & Err.Description & Chr(34)
    sqla = "INSERT INTO [Event log] ([Event])VALUES(" & errname & ") ; "
 
    DoCmd.RunSQL sqla
GoTo exit_sub
End Sub
Combo10 is the first box for users to select the building (if they want too) and Combo12 is for Item. All other Combos are for the third selection.
Also for the next part a form has to be opened using the contents of each combo box as a filter - to simplify getting the third criteria all combo boxes for the third selection input a hidden unbound text box and that is used instead.
To filter the combo boxes and to get to the point (Sorry!) in the combo boxes Row Source property put the following code
Code:
SELECT DISTINCT Furniture.Item FROM Furniture WHERE (((Furniture.Building)=IIf(IsNull([Forms]![Search furniture]![Combo10]),[Building],[Forms]![Search furniture]![Combo10])));
This code came from combo12, dont forget to change the form and control names.
 

Users who are viewing this thread

Back
Top Bottom