OpiesDad
OpiesDad

Reputation: 3435

Open Access Form with Filter VBA

I'm having trouble opening a form using a filter. The code that I am trying to run is this:

DoCmd.OpenForm "MyForm", , "[ID] = " & Me.MyListBox.Column(0), , acFormEdit, acDialog

When I run this line, it opens the form, but does not apply the filter.

The FilterOnLoad property is set to True, and I have verified that it is true in the On Load event.

While in the Form_Load event, Me.FilterOn = False and Me.Filter = "".

What happened to the filter? What am I doing wrong? I've tried setting the Me.FilterOn property to True via VBA and then saving the form, but when I open the form again, it is reset to False.

If I put the filter in using VBA and then set the Me.FilterOn property to True, the form correctly filters. I have verified that the correct value is there for "Me.MyListBox.Column(0)"

Upvotes: 2

Views: 15661

Answers (1)

HansUp
HansUp

Reputation: 97101

Here is what the DoCmd.OpenForm Method help topic has to say about the FilterName parameter:

A string expression that's the valid name of a query in the current database.

But you're not giving it the name of a query. I think you actually want the WhereCondition parameter instead:

DoCmd.OpenForm FormName:="MyForm", _
    WhereCondition:="[ID] = " & Me.MyListBox.Column(0), _
    DataMode:=acFormEdit, _
    WindowMode:=acDialog

Upvotes: 3

Related Questions