Results 1 to 5 of 5
  1. #1
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402

    Dlookup multiple text criteria from vba variables

    Hello again, I'm trying to get this dlookup working -



    it is using to vba variables as the critera - first name and last name, the dlookup works ok with either one but i have to use both as we have lots of people with the same first or last names.

    i have tried various combinations of single and double quotes all over the place and just got confused by it all, the line below is the closest i've got so far. but it returns a syntax error of -
    syntax error in string in query expresion '[Firstname]='Firstname1' AND [Lastname] = 'Lastname1'


    tt = DLookup("[Code #]", "Employees", "[First Name]= '" & t1 & "' AND " & "[Last Name] = '" & t2 & "")

    I do hope someone can help with this...

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    Missing apostrophe delimiter at the end. There is an unnecessary concatenation but that should not cause error.

    Really should not use spaces and special characters/punctuation (underscore is exception) in naming convention. Better would be CodeNum or Code_Num.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You don't have a single quote/apostrophe between the double quotes at the end.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402
    I have fixed it thanks,

    tt = DLookup("[Code #]", "Employees", "[First Name]= '" & t1 & "' And [Last Name] = '" & t2 & "'")

    As to all the comments I see here about not using spaces in names, well you just have to put the [ ] around the field and it works fine. I have never had a problem doing this and I think its strange that people are advising against it.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    Personally, I find it annoying to always have to remember to type []. This can be especially frustrating for newbies. But since you are aware of the fix and happy with your structure, so be it.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 9
    Last Post: 04-18-2014, 08:51 PM
  2. Replies: 2
    Last Post: 01-22-2014, 09:38 AM
  3. Using DLookup with multiple criteria
    By halt4814 in forum Access
    Replies: 2
    Last Post: 04-08-2013, 12:26 PM
  4. Replies: 11
    Last Post: 04-30-2012, 07:22 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