Results 1 to 3 of 3
  1. #1
    Join Date
    May 2019
    Posts
    65

    Dlookup with multiple criteria

    Before I add a new contact from my form, I want to search to see if any contact exists using the first and last name. I can successfully search with the Dlookup in the code as shown in number 1, below however, when I try to replace the text with variables, #2, below, I get:


    Run-Time Error 2471 - The expression you entered as a query parameter produced this error: 'VarFirst'


    #1 Successful DLookup

    Code:
    Private Sub CLastName_AfterUpdate()
    'Purpose to find if a Contact Name exists and to use it
    'or continue to create a new one.
    
    
        Dim varFirst As String
        Dim varLast As String
        Dim varTitle As String
        
          
        varLast = Me.CLastName
        varFirst = Me.CFirstName
        
        Debug.Print varFirst
        Debug.Print varLast
     
        varTitle = DLookup("[CTitle]", "tblContacts", "[CLastName] = 'Ale' And [CFirstName] = 'Dan'")
        
        Debug.Print varTitle
    
    
    End Sub
    #2 Unsuccessful

    Code:
    Private Sub CLastName_AfterUpdate()
    'Purpose to find if a Contact Name exists and to use it
    'or continue to create a new one.
    
    
        Dim varFirst As String
        Dim varLast As String
        Dim varTitle As String
        
          
        varLast = Me.CLastName
        varFirst = Me.CFirstName
        
        Debug.Print varFirst
        Debug.Print varLast
     
        varTitle = DLookup("[CTitle]", "tblContacts", "[CLastName] = varLast And [CFirstName] = varFirst")
        
        Debug.Print varTitle
    
    
    End Sub
    I have tried all sorts of variations with the criteria, but non work.
    Any Suggestions

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    strings MUST have string delimiters:

    …, "[CLastName] ='" & varLast & "' And [CFirstName] ='" & varFirst & "'")

    'but people NAMES can have a problem with O'Malley, etc. if single quote is used above,
    so:

    Q = """"
    …, "[CLastName] = " & Q & varLast & Q & " And [CFirstName] =" & Q & varFirst & Q )

  3. #3
    Join Date
    May 2019
    Posts
    65
    Thank you!!! That worked perfectly.

    Jeff

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

Similar Threads

  1. multiple criteria in a dlookup
    By xmattxman in forum Programming
    Replies: 1
    Last Post: 04-04-2019, 08:21 AM
  2. DLookup Multiple Criteria
    By sergi117 in forum Access
    Replies: 1
    Last Post: 09-17-2018, 10:49 AM
  3. DLookup with multiple criteria
    By amai in forum Access
    Replies: 2
    Last Post: 12-20-2015, 02:10 PM
  4. Using DLookup with multiple criteria
    By halt4814 in forum Access
    Replies: 2
    Last Post: 04-08-2013, 12:26 PM
  5. Multiple criteria in DLookup
    By crowegreg in forum Forms
    Replies: 7
    Last Post: 06-22-2011, 01:47 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