none
ApplyFilter command run from Form_Load fails with error 2491

    Question

  • I am getting this error

    Error: 2491 "The action or method is invalid because the form or report isn't bound to a table or query".

    when I run this code, which is invoked from inside the Form_Load event. The form does have a data source (a table). Before this code, I insert a new a record in the bound table via a recordset and now an attempting to position on that record with the ApplyFilter command. The code fails when run normally. When I single step through the code it always works fine. Any ideas?

       On Error GoTo EX  ' <====== IS TRIGGERING 
       DoCmd.ApplyFilter , "[ID]='" & strSubj & "'"  'Triggers Current event
       On Error GoTo ER
       Me!SubjectId = strSubj  
       Me!SubjectId.SetFocus
       Call SetMode("New")
    EX:
       MsgBox "butNew error " & Nz(Err.Description, "")
       Exit Sub
    ER:

    Thursday, December 06, 2012 3:45 PM

Answers

All replies

  • Found solution in this thread:

    http://social.msdn.microsoft.com/Forums/en-AU/accessdev/thread/18796b11-5776-47c5-9917-8887e3118bd2

    which suggests to use Me.Recordsource = with a WHERE clause instead of the ApplyFilter command.

    The ApplyFilter command has always worked for me before so I don't know what caused the problem in this case.

    Thursday, December 06, 2012 4:33 PM
  • I marked the above as the answer but I have since found it is not. Using a recordsource with a WHERE clause avoids the 2491 error and allows the form to display with my ID in the appropriate control but I am not positioned on the record. When I type in data, my Undo button does not become active and when I save the record, none of the entered data is saved. I tried setting Me.recordsource in both Form_Open and Form_Load but I still get the 2491 error when I do the ApplyFilter command.

    Anyone got any ideas? I am totally stuck on this.

    Thursday, December 06, 2012 9:34 PM
  • I marked the above as the answer but I have since found it is not. Using a recordsource with a WHERE clause avoids the 2491 error and allows the form to display with my ID in the appropriate control but I am not positioned on the record. When I type in data, my Undo button does not become active and when I save the record, none of the entered data is saved. I tried setting Me.recordsource in both Form_Open and Form_Load but I still get the 2491 error when I do the ApplyFilter command.

    Anyone got any ideas? I am totally stuck on this.

    Hi AllTheGood...,

    What happens if you use:

            Me.Filter = "[ID]='" & strSubj & "'"
            Me.FilterOn = True

    instaed of ApplyFilter?

    Is [ID] indeed a text-field? (In my syntax ID is always a Long).

    Imb.

    Thursday, December 06, 2012 10:00 PM
  • What version of Access? 2010? I find it is more buggy than prior versions.

    Aside from that, my guess is that it has to do with some of the other code that you have running.

    Can you post all of the code that is running when the form opens, including your SetMode and OnCurrent event.

    Have you tried sprinkling in some DoEvents statements? That sometimes solves timing problems, evidenced by the fact that it runs when you single step.

    Thursday, December 06, 2012 10:09 PM
  • I think you may have hit on the solution. My first test with this code change worked. Yes [ID] is a text field; it's the primary key.

    The puzzling thing about this error is that I open this form from two other forms. From one it performs flawlessly. From the other, I get this 2491 error. And as far as I can see the two logic paths are identical. The only difference is the form that the other form is opened from.

    The only thing that looked suspicious to me was that there is a DLookup to the same table the form is bound to and it is in the Form_Load event before the call to the procedure that does the ApplyFilter. However, when I commented out the DLookup, I still got the 2491 error.

    This is Access 2010.

    Thursday, December 06, 2012 11:36 PM
  • Hello AllTheGoodNamesWereTaken,

    I agree with Alphonse G that it is likely related to other code that is running but which code could be interesting to find.

    You mentioned that the code works when called from one form but not form the other. What happens if you just open the form directly from the navigation pane?

    My only guess at this point is that because you are adding a record to the underlying table after the form has started loading maybe Access isn't able to find that record which may be why you are having a problem with setting the record source of the form to a SQL statement.

    My suggestion would be to try to reproduce the issue in a simple form.
    First I would create a new bound form based on the same table.
    Add code to the Form_Load even to filter for an existing record and confirm this works.
    Then add code to add the new record to the table via a record set and see if you can still filter on an existing record.
    Change the code to try to filter for the newly added record.

    Assuming all of this works, because it seems to be a model you follow often, I would then start looking at the code in the Form_Load and Form_Open events.
    You could also see if it is related to the calling form by calling this new test from in the same way you call the problematic form to see if you can reproduce the issue.

    Best Regards,

    Donald M.
    Microsoft Online Community Support
    --------------------------------------------------------------------------------
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Wednesday, December 12, 2012 9:48 PM
  • Hi AllTheGoodNamesWereTaken,

    I temporarily marked the replies as answers and you can unmark them if they provide no help.

    Thanks for your understanding and have a nice day.

    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 27, 2012 6:35 AM