Filter on a form

capogna1

Member
Local time
Yesterday, 21:44
Joined
Aug 8, 2019
Messages
46
HI I created combo boxes on my form to select what I want to filter and I used the following:

Code:
Sub RequeryForm()
Dim strSQL As String
strSQL = ""

If Len("" & Me!cboTypeOfEncounter) > 0 Then
    strSQL = "[Type Of Encounter] = '" & Me!cboTypeOfEncounter & "'"
End If

If Len("" & Me!cboReviewer) > 0 Then
    If Len(strSQL) > 0 Then
    strSQL = strSQL & "And"
    End If
    strSQL = strSQL & "[Reviewer] = '" & Me!cboReviewer & "'"
End If

If Len(strSQL) = 0 Then
    Me.FilterOn = False
Else
 
    

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


End Sub

Private Sub cboReviewer_BeforeUpdate(Cancel As Integer)

End Sub

Private Sub cboTypeOfEncounter_AfterUpdate()

End Sub

Private Sub Form_AfterUpdate()
RequeryForm
End Sub

But it's not filtering, anyone can help?
Is there an easier way? Like from Criteria in the query to get a list to pick from? Trying to make something for the user different from applying filters from the query.
Thanks in advance.
 
Consider posting a sample db with test data, if you can.
 
So what are the bound fields for your combos?, numeric or strings?
You are catering for strings?

Use Debug.Print strSQl to see what you actually have, not what you think you have?
 
So what are the bound fields for your combos?, numeric or strings?
You are catering for strings?

Use Debug.Print strSQl to see what you actually have, not what you think you have?
Unbound and Row source: SELECT DISTINCT T_Encounter.Type_Of_Encounter FROM T_Encounter ORDER BY T_Encounter.Type_Of_Encounter;
 
So show the output of the debug.print as I mentioned?, just before you try and use it.
Just noticed that you have no space either side of the AND :(
Again a debug.print reveals so much more.
 
Unbound and Row source: SELECT DISTINCT T_Encounter.Type_Of_Encounter FROM T_Encounter ORDER BY T_Encounter.Type_Of_Encounter;
If I do Debug.print it ask to choose a macro
 
This thread may provide inspiration:-

 
I made a "Universal Filter" ... See here:-

Universal Filter - Nifty Access​


 
So what are the bound fields for your combos?, numeric or strings?
You are catering for strings?

Use Debug.Print strSQl to see what you actually have, not what you think you have?
Not getting nothing from Debug.Print
 
You are looking in the immediate window?
You do know how to use debug.print <variable> I hope?
 
I Ctrl + G put my cursor in immediate window then F5
I get this
1665000028562.png
 
No.
You put
Code:
Debug.Print strSQL
before
Code:
If Len(strSQL) = 0 Then
    Me.FilterOn = False
Then you go to the immediate window, and copy and paste the output back here within code tags.

A simple example
1665000388859.png


and the result
1665000471551.png
 
You have to run that code?
Put a prefix with that variable like
Debug.Print "sql is " & strsql
Then copy and paste the output back here.
 
Why not just walk through the code by setting a breakpoint and then use F8 to step through line by line, and see where it goes?
 
Why not just walk through the code by setting a breakpoint and then use F8 to step through line by line, and see where it goes?
Code:
Sub RequeryForm()
Dim strSQL As String
strSQL = ""

If Len("" & Me!cboTypeOfEncounter) > 0 Then
    strSQL = "[Type Of Encounter] = '" & Me!cboTypeOfEncounter & "'"
End If

If Len("" & Me!cboReviewer) > 0 Then
    If Len(strSQL) > 0 Then
    strSQL = strSQL & "And"
    End If
    strSQL = strSQL & "[Reviewer] = '" & Me!cboReviewer & "'"
End If

Debug.Print "sql is " & strSQL

If Len(strSQL) = 0 Then
    Me.FilterOn = False
Else
    Me.Filter = strSQL
    Me.FilterOn = True
End If


End Sub

Private Sub cboReviewer_BeforeUpdate(Cancel As Integer)

End Sub

Private Sub cboTypeOfEncounter_AfterUpdate()

End Sub

Private Sub cboTypeOfEncounter_Click()

End Sub

Private Sub Form_AfterUpdate()
RequeryForm
End Sub

This is what it did and nothing happens with F8, if I run it open the macro dialog box
 
Upload a sample of your DB. We are at post #19 for a simple filter issue. :(
Normally you would set a breakpoint on the first executable line of code in that sub, then step through with F8. That is how I do it, or F5 to another breakpoint.
 

Users who are viewing this thread

Back
Top Bottom