Results 1 to 6 of 6
  1. #1
    AccessRockie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2019
    Posts
    11

    Dlookup issue

    Hi All,

    I can't seem to get this Dlookup function to work.
    Basically, I have table: "Registry", the field within the table I want to lookup is "Reference". The form is called "AddToRegistry" and the text inputted in the textbox "Reference" I want to check it against the field within the table for duplicates before saving. I had it working at one point but not anymore.

    Any ideas.
    Thanks in advanced.


    Dim Answer As Variant
    Answer = DLookup("[Reference]", "Registry", "[Reference] =" & Forms![AddToRegistry]!Reference)

    If Not IsNull(Answer) Then
    MsgBox "Reference Number Already Exists!" & vbCrLf & "Check the Number and Try Again.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"

    Cancel = True
    Me.Reference.Undo

    Else:
    End If

    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    If the field data type is TEXT, then try enclosing the form reference with single quote delimiters
    Code:
    Dim Answer As Variant
    
    Answer = DLookup("[Reference]", "Registry", "[Reference] ='" & Forms![AddToRegistry]!Reference)& "'"




    Instead of DLookup(), I would think about using query or DCount(). (You can use the DCount function to determine the number of records that are in a specified set of records.)

  3. #3
    AccessRockie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2019
    Posts
    11
    Thanks for the reply.
    I tried the code below.
    Now I'm getting a Run-time error '3075' Syntax error in string in query expression '[Reference] ='A123456'.

    Quote Originally Posted by ssanfu View Post
    If the field data type is TEXT, then try enclosing the form reference with single quote delimiters
    Code:
    Dim Answer As Variant
    
    Answer = DLookup("[Reference]", "Registry", "[Reference] ='" & Forms![AddToRegistry]!Reference)& "'"




    Instead of DLookup(), I would think about using query or DCount(). (You can use the DCount function to determine the number of records that are in a specified set of records.)

  4. #4
    roaftech is offline Advanced Beginner
    Windows 10 Access 2003
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    65
    I have been using the DLookup function recently - it can be a pain to get right.
    This may sound unlikely but I have found that the full stop (.) and the exclamation mark (!) are not quite interchangeable so I would be inclined to change your second ! in your original formula to a . and see if that makes a difference.

  5. #5
    AccessRockie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2019
    Posts
    11
    Tell me about it....a pain it is.
    Still getting the same error.

    Quote Originally Posted by roaftech View Post
    I have been using the DLookup function recently - it can be a pain to get right.
    This may sound unlikely but I have found that the full stop (.) and the exclamation mark (!) are not quite interchangeable so I would be inclined to change your second ! in your original formula to a . and see if that makes a difference.

  6. #6
    AccessRockie is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2019
    Posts
    11
    FIXED!!! Thanks I used the Dcount method.
    Worked like a charm.

    Thanks again.



    Quote Originally Posted by AccessRockie View Post
    Tell me about it....a pain it is.
    Still getting the same error.

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

Similar Threads

  1. VBA, DLookup issue
    By jtm013 in forum Programming
    Replies: 6
    Last Post: 03-06-2015, 03:14 PM
  2. DLookup Issue
    By sdel_nevo in forum Programming
    Replies: 10
    Last Post: 05-18-2013, 08:51 AM
  3. Dlookup issue
    By Gilgamesh in forum Forms
    Replies: 5
    Last Post: 12-22-2012, 10:26 PM
  4. DLOOKUP issue
    By gemadan96 in forum Forms
    Replies: 7
    Last Post: 11-01-2012, 06:21 PM
  5. DLookup issue
    By seth1685 in forum Programming
    Replies: 5
    Last Post: 01-12-2012, 08:55 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