Results 1 to 7 of 7
  1. #1
    caddcop is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Upper Marlboro, Maryland, USA
    Posts
    57

    How can I use Dlookup on a table record that contains a comma?

    Just to reiterate, I have a query that contains a full name in LastName, FirstName format and I need (really want) to use that column as my match fields in a Dlookup function.
    However, that seems to be an issue with the tool.

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    That shouldn't be a problem. What is the syntax you've been trying to use (post the real code).

  3. #3
    Rhino373 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    May 2011
    Posts
    65
    You have to use single quotes around your criteria like "FullName = 'Frost, Jack'"

    Naturally, you shouldn't keep this field like such. You can use a constructed field with a concatenate later to show it Last, First to show it...... blah blah blah but that's how you do it.

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by Rhino373 View Post
    You have to use single quotes around your criteria like "FullName = 'Frost, Jack'"
    Although I would argue for double quotes (probably with Chr(34) ) because last names can be notorious for single quotes which would throw it off as well (O'Brien, O'Neil, etc.).

    Naturally, you shouldn't keep this field like such. You can use a constructed field with a concatenate later to show it Last, First to show it
    I agree with that wholeheartedly. Separate fields for separate data. It is easier to pull them together than break them apart.

  5. #5
    caddcop is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Upper Marlboro, Maryland, USA
    Posts
    57
    Here is my code:
    Code:
        Dim varX As Variant
        If IsNull(Me.Role) Then
            varX = DLookup("[Assignment]", "[StaffMembers_FullNames]", "[FullName] = " & Me.Text_NameField_Hidden)
        End If
    where Assignment is the field that I want to grab the matching record from, [StaffMembers_FullNames] is the source query, [FullName] is the linking field and Me.Text_NameField_Hidden is a text box on my form whose control source is the field that contains the full name as explained.

    However, the suggestion to enclose my string with single quotes did the trick.
    Code:
    varX = DLookup("[Assignment]", "[StaffMembers_FullNames]", "[FullName] = '" & Me.Text_NameField_Hidden & "'")
    It's hard to tell, but there is a single quote followed by a double quote before the first & and a single quote inside of two double quotes after the second & and now the DLookup does not throw an error and, the value of varX is the value I am seeking.

  6. #6
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by caddcop View Post
    However, the suggestion to enclose my string with single quotes did the trick.
    Again, because this is with names I would HIGHLY suggest not using single quotes and use Double Quotes like this:
    Code:
    varX = DLookup("[Assignment]", "[StaffMembers_FullNames]", "[FullName] = " & Chr(34) & Me.Text_NameField_Hidden & Chr(34))

  7. #7
    caddcop is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Feb 2011
    Location
    Upper Marlboro, Maryland, USA
    Posts
    57
    I've already been bitten by name fields with single and double quotes in them.
    In my last problem, I ended up checking for single quotes or double quotes and having two queries to run depending upon if it found one or the other. I also know that this particular solution will fail if a name has both - something I can only hope, does not happen.

    Unfortunately, I am limited by the fields in the tables as setup by a commercial product that we are using access to extend its reach and capabilities.

    I was able to use your code, so far, with no problems.

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

Similar Threads

  1. Populate table after DLookup is performed
    By OMGsh Y did I say Yes in forum Forms
    Replies: 16
    Last Post: 03-21-2011, 11:25 AM
  2. Replies: 2
    Last Post: 02-14-2010, 01:38 PM
  3. How do I get dlookup values to store in table
    By rpmyhero in forum Access
    Replies: 1
    Last Post: 11-25-2009, 05:57 AM
  4. Replies: 2
    Last Post: 11-04-2009, 09:45 PM
  5. Replies: 1
    Last Post: 08-21-2009, 06:52 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