Results 1 to 3 of 3
  1. #1
    graffiti is offline Newest Newbie ever
    Windows XP Access 2003
    Join Date
    Nov 2011
    Location
    Worthing, south coast of the UK
    Posts
    21

    Newbie. How do I use DLookup with a variable please?

    Hi.

    Another very simple one for the font of all knowledge. I have the following code which works perfectly:

    Code:
     
    'Return the ID of the matching UserName
        varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = 'test1'")
    However, I obviously don't want to hardcode the 'test1' into the expression. The following variations on using a variable fail:

    Code:
     
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = " & varUserName)
     
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = varUserName")
     
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = 'varUserName'")
    varUserName is a string variable which has been earlier retrieved from a text box and I know it has the correct content by using the debug step through system and watching the variable(s).



    How should this be phrased correctly please? Yes, I am working through this by trial and error. Thanks.

    Mike.

  2. #2
    Toyman is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Posts
    233
    Quote Originally Posted by graffiti View Post
    Hi.

    Another very simple one for the font of all knowledge. I have the following code which works perfectly:

    Code:
     
    'Return the ID of the matching UserName
        varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = 'test1'")
    However, I obviously don't want to hardcode the 'test1' into the expression. The following variations on using a variable fail:

    Code:
     
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = " & varUserName)
     
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = varUserName")
     
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = 'varUserName'")
    varUserName is a string variable which has been earlier retrieved from a text box and I know it has the correct content by using the debug step through system and watching the variable(s).

    How should this be phrased correctly please? Yes, I am working through this by trial and error. Thanks.

    Mike.
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = '" & varUserName & "'")

  3. #3
    graffiti is offline Newest Newbie ever
    Windows XP Access 2003
    Join Date
    Nov 2011
    Location
    Worthing, south coast of the UK
    Posts
    21
    I knew it was simple, when you know how. Note to self: research syntax more.

    Thanks very much.

    Mike.

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

Similar Threads

  1. Replies: 3
    Last Post: 12-21-2011, 10:49 AM
  2. Use a variable in a DLOOKUP?
    By leftylee in forum Forms
    Replies: 9
    Last Post: 07-27-2011, 02:00 PM
  3. Form Design Question with DLookup (newbie)
    By wongc61 in forum Access
    Replies: 4
    Last Post: 07-08-2011, 03:22 PM
  4. Help Using Variable in DLookup Statement
    By bcmarshall in forum Access
    Replies: 9
    Last Post: 12-02-2010, 12:44 AM
  5. Newbie Dlookup problem
    By opopanax666 in forum Programming
    Replies: 7
    Last Post: 08-13-2010, 05: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