Filter on a form (1 Viewer)

capogna1

Member
Local time
Today, 04:01
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:01
Joined
Oct 29, 2018
Messages
21,473
Consider posting a sample db with test data, if you can.
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
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?
 

capogna1

Member
Local time
Today, 04:01
Joined
Aug 8, 2019
Messages
46
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;
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
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.
 

capogna1

Member
Local time
Today, 04:01
Joined
Aug 8, 2019
Messages
46
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
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:01
Joined
Jul 9, 2003
Messages
16,282
This thread may provide inspiration:-

 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 09:01
Joined
Jul 9, 2003
Messages
16,282
I made a "Universal Filter" ... See here:-

Universal Filter - Nifty Access​


 

capogna1

Member
Local time
Today, 04:01
Joined
Aug 8, 2019
Messages
46
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
You are looking in the immediate window?
You do know how to use debug.print <variable> I hope?
 

capogna1

Member
Local time
Today, 04:01
Joined
Aug 8, 2019
Messages
46
I Ctrl + G put my cursor in immediate window then F5
I get this
1665000028562.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
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?
 

capogna1

Member
Local time
Today, 04:01
Joined
Aug 8, 2019
Messages
46
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:01
Joined
Sep 21, 2011
Messages
14,301
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.
 

capogna1

Member
Local time
Today, 04:01
Joined
Aug 8, 2019
Messages
46
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.
 

Attachments

  • Medical Records Review - V41 - Copy NOPE.accdb
    6.5 MB · Views: 99

Users who are viewing this thread

Top Bottom