Results 1 to 5 of 5
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    Building a DLookup statement correctly

    In the two db's that I have attached, one works (the smaller db) and one I am working on db (the larger db)



    When I am working on the correct functioning db, I come across the lines of code:

    Code:
     '-- Combo of names to assign ticket to
        stWho = Me.cboAssignee
        stWhere = "tblUsers.strUserID = " & "'" & stWho & "'"
        '-- Looks up email address from TblUsers
        varTo = DLookup("[strEMail]", "tblUsers", stWhere)
    Now I will address a couple of things. First, the record source for the form frmEmail is a query qryMeeting_Personnel. So I guess that I will have to substitute when I make out the
    stWhere statement on the larger db and put it in quotes. Again there is no table, my record source is qryMeeting_Personel. Now I must also build the varTo statement. So I believe this it should be

    varTo = DLookup("email address","qryMeeting_Personnel',stWhere)
    provided StWhere was assumed previously defined correctly.

    The files of the two dbs one complete and working; that one is a work in progress, are attached.

    Please tell me if I did this correctly.
    Attached Files Attached Files

  2. #2
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Not sure why you have to use all those variables instead of just spelling it out on 1 line:
    varTo = DLookup("[strEMail]", "tblUsers", "Where [strUserID] = '" & Me.cboAssignee & "'")

  3. #3
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    It is just for readability. I thought breaking it down would simplify the coding. Maybe, I was wrong.

    It does not seem to hurt anything, though.

    Would not doing it be an improvement or not?

    Respectfully,

    Lou Reed

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Quote Originally Posted by Bulzie View Post
    Not sure why you have to use all those variables instead of just spelling it out on 1 line:
    varTo = DLookup("[strEMail]", "tblUsers", "Where [strUserID] = '" & Me.cboAssignee & "'")
    That wouldn't work with the word "Where" in it, would it?

    @Lou, to me it's personal preference. I wouldn't use the variable for a simple 1 field criteria like this, I would if it gets longer, has more fields, etc.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Yes my bad, take the "Where " out. I think it is much easier to read a single line of text with the actual fields instead of reading variables and figuring out what they reference. But as Paul said, you can do it how you like as long as it works how you need.

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

Similar Threads

  1. Case statement doesn't work correctly
    By Cecil in forum Access
    Replies: 3
    Last Post: 05-16-2016, 03:21 PM
  2. Replies: 7
    Last Post: 11-15-2013, 10:28 AM
  3. Replies: 1
    Last Post: 08-29-2013, 06:14 AM
  4. DLookup not functioning correctly - any ideas please?
    By shabbaranks in forum Programming
    Replies: 2
    Last Post: 02-02-2012, 09:09 AM
  5. Replies: 16
    Last Post: 08-19-2010, 01:40 PM

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