Results 1 to 4 of 4
  1. #1
    perryc is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2019
    Posts
    122

    VBA Code to see if a record exists in a Query

    I have a query, it filters out Zip codes in a table (active or inactive).

    If I want to write a VBA code in an after update event of a free field, please help how to accomplish that.

    i.e. QueryZip has 3 zips, 23111, 23112 & 23113 (but 23113 is active, therefore does not show up in the QueryZip). Without opening QueryZip, I have a form to look for if 23113 exists in the QueryZip or not.

    Hope I explain ok. Ty in advance.

    Sincerely,
    Perry

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,913
    Use DlookUp() ?

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Not really sure as to your exact need here...but

    Code:
    If DCount("*","QueryZip","ZipCodeFieldName = 23113" > 0 Then
      MsgBox "Zip Code 23113 Exists in the Query!"
    End If
    will pop up a MessageBox if 23113 exists in the Query.

    Note that you'd have to replace ZipCodeFieldName with the actual name of your field...and that the 23113 is hard coded, here...you'd have to modify this syntax slightly if you were pulling the Zip Code from somewhere...such as a search box on a Form.

    Code:
    If DCount("*","QueryZip","ZipCodeFieldName =" & Me.SearchBoxName) > 0 Then
      MsgBox "Zip Code " & Me.SearchBoxName & " Exists in the Query!"
    End If
    Linq (from Zip Code 23237) ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    perryc is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2019
    Posts
    122
    Ty. This works. I got another dumb question.

    For some reasons, when I then tried to clear out the fields associated with the zip, e.g. City, by this: me.city = "", well, it looked up the Zip for me to change again. Why?

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

Similar Threads

  1. Delete from Query B where record exists in Query A
    By patchesohouli in forum Queries
    Replies: 3
    Last Post: 04-29-2016, 01:04 PM
  2. Replies: 3
    Last Post: 08-26-2015, 12:58 AM
  3. Code to return 'No Record exists'
    By coach32 in forum Programming
    Replies: 2
    Last Post: 07-28-2015, 07:29 AM
  4. VBA code to check if a record already exists
    By fra90 in forum Programming
    Replies: 3
    Last Post: 11-20-2013, 11:20 AM
  5. if record exists...conditional query criteria
    By mbryanr in forum Queries
    Replies: 2
    Last Post: 02-12-2010, 11:50 AM

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