form filter via vba/macro (1 Viewer)

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
Good morning guys,
I would like to have the form filtered using a button. For example, if I press filter 1, NM should be filtered to ZN.(Form.png)
I've read many threads but couldn't get it to work...
May some of you guys can help me.

Appreciate it!

Best regards
Georg
 

Attachments

  • Form.PNG
    Form.PNG
    25.7 KB · Views: 107
  • Table.PNG
    Table.PNG
    10.8 KB · Views: 106

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
Private Sub Filter1()
Me.Filter = "NM = 'ZN'"
Me.FilterOn = True
End Sub



I've to admit that i'm not very confident with vba in Access.
 

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
I wrote it in the VBA Code of button "Filter 1".
 

bob fitz

AWF VIP
Local time
Today, 08:38
Joined
May 23, 2011
Messages
4,717
Have checked that the code does actually fire when the button is clicked
 

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
Does it matter if the subform is a query?
 

bob fitz

AWF VIP
Local time
Today, 08:38
Joined
May 23, 2011
Messages
4,717
PMFJI, but if you had put that in the click event of the button, the name of the Sub would reflect that.?
Indeed, which is why I asked about the code actually running as I would expect an error message if it did. From the picture posted by the OP the button appears to be on a main form
 

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
The query is called Test1 and the Sub form is called Test1-Unterformular
 

bob fitz

AWF VIP
Local time
Today, 08:38
Joined
May 23, 2011
Messages
4,717
Try

Me!Subform1.Form. [Test1-Unterformular].Filter = "NM = 'ZN'"
Me!Subform1.Form. [Test1-Unterformular].FilterOn = True
 

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
What does Subform1.form mean?
Is it the Name of the form i want the filter button on?
 

bob fitz

AWF VIP
Local time
Today, 08:38
Joined
May 23, 2011
Messages
4,717
No. Sorry, Should be the name of the subform control on the main form. So try
Me![Test1-Unterformular].Form.Filter = "NM = 'ZN'"
Me![Test1-Unterformular].Form.FilterOn = True
 

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
It worked!!!!
Thank you very much, @bob fitz . You just saved my day :D
Appreciate it!
 

georg7

Registered User.
Local time
Today, 01:38
Joined
Sep 3, 2019
Messages
84
Do you know how to connect it with a drop down bar? Where I can pick the NM Value that should be the criteria ?
 

Users who are viewing this thread

Top Bottom