Results 1 to 7 of 7
  1. #1
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51

    VBA filter retaining old value

    In Access 2010 I am trying to make a form where the user can enter a userID and the form will show the information for that one userID.
    Right now I have a form with fields for the information. At the top I have a field to enter the UserID to search for.

    The VBA for the UserID search box is:
    Code:
    Private Sub IDLookup_Exit(Cancel As Integer)
        Me.Filter = ""
        Me.FilterOn = False
        Me.Filter = "([ID] = Forms![Entry Form]![IDLookup])"
        Me.FilterOn = True
    End Sub
    It works great the first time I search for a userID. The problem is that when I type in a second userID, the filter doesn't clear. It stays with the first one I typed. I thought this was a simple code to clear the filter and set it to the new userID entered. Unless I close the form and open it again, the code will never look up a new userID. What am I doing wrong?

    Thank you

  2. #2
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Nigelbloomy -

    Have you tried putting your code into the AfterUpdate Event of the IDLookup control?

    All the best,

    Jim

  3. #3
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    I originally had it in the after update event and it results in the same problem. 1st entry gets looked up perfectly. Every entery after that flashes a little like it is filtering again, but it keeps filtering for the first userID.

  4. #4
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Nigelbloomy -

    I might try...
    Me.Filter = ""
    Me.FilterOn = False
    Me.Filter = "([ID] = Forms![Entry Form]![IDLookup])"
    Me.FilterOn = True
    Me.Requery

    and see if that changes anything. One other question, is this a continuous form?

    Jim

  5. #5
    nigelbloomy is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2012
    Posts
    51
    Adding the requery worked! I haven't used continuous forms before so I don't think my form is continuous. I don't know why Access needs the requery to clear out the old filter. I was also able to delete the first two lines of code and it still works great. It does have to be in the afterupdate event though. The code goes crazy in the exit event where I had it.

    Code:
    Me.Filter = "([ID] = Forms![Entry Form]![IDLookup])"
        Me.FilterOn = True
        Me.Requery
    Thank you for the help.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    When you have an expression like "([ID] = Forms![Entry Form]![IDLookup])", everything within the quotes becomes a text string. So, Access thinks you want "WHERE [ID] equals the string "Forms![Entry Form]![IDLookup]".
    You need to concatenate the value of the control (with appropriate delimiters) to the property.

    If [IDLookup] is numeric, then you would use:
    Code:
    Me.Filter = "[ID] = " & Forms![Entry Form]![IDLookup]

    If [IDLookup] is text, then you would use:
    Code:
    Me.Filter = "[ID] = '" & Forms![Entry Form]![IDLookup] & "'"
    Expanded, the text in RED at the end looks like " ' ".

  7. #7
    ketbdnetbp is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Mar 2011
    Location
    Midwest
    Posts
    254
    Steve - Thanks, I overlooked that part. Jim

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Retaining a Field Value
    By Lupson2011 in forum Forms
    Replies: 4
    Last Post: 12-05-2011, 09:16 AM
  2. Retaining current record.
    By mnsemple83 in forum Forms
    Replies: 10
    Last Post: 08-30-2011, 08:58 AM
  3. retaining data on a form
    By appleb007 in forum Forms
    Replies: 1
    Last Post: 06-07-2011, 11:07 AM
  4. Object variable not retaining value
    By tuna in forum Programming
    Replies: 0
    Last Post: 05-21-2010, 05:38 PM
  5. Retaining format
    By tlmackey in forum Import/Export Data
    Replies: 0
    Last Post: 02-25-2010, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums