Why I Cant set the FilterOn property in form_open?

I try to set a filter of an continuous form in access vba. This is the code I use:

Private Sub Form_Open(Cancel As Integer)
  
  Dim filter As String
  filter = "1 = 0"  ' "1=0" is just for testing purpurses
  Me.filter = filter
  Me.FilterOn = True
  Debug.Print Me.FilterOn & "; " & Me.filter

end sub

The output is:

False; 1 = 0

and the filter is not used.

Why does this not work? And is there a way to set and activate the filter before the form is shown?

1 answer

  • answered 2022-05-04 11:43 Gener4tor

    I found out that the reason is. I use a framework where I set the recordsource later. While there is no recordsource, "Me.FilterOn = True" simply does not work. The following code does work:

    Private Sub Form_Open(Cancel As Integer)
      
      Me.RecordSource = "select * from MyTable"
      
      Dim filter As String
      filter = "1 = 0"  ' "1=0" is just for testing purpurses
      Me.filter = filter
      Me.FilterOn = True
      Debug.Print Me.FilterOn & "; " & Me.filter
    
    end sub
    

    and the output is:

    True; 1 = 0
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum