Results 1 to 3 of 3
  1. #1
    szucker1 is offline Novice
    Windows XP Access 2000
    Join Date
    Jun 2011
    Posts
    18

    DLookup in strSQL for OpenRecordset

    I keep getting the error "Microsoft Access can't find the field 'I' referred to in your expression." when I try to run the following code:



    Code:
     
        Dim rst1 As DAO.Recordset
        Dim strSQL As String
        strSQL = "SELECT tblAssignments.Position " & _
        "FROM (tblAssignments " & _
        "INNER JOIN [tblTournament Levels] " & _
        "ON tblAssignments.Level = [tblTournament Levels].Level) " & _
        "INNER JOIN [tblReferee Credit Rates] " & _
        "ON [tblTournament Levels].Level = [tblReferee Credit Rates].Level " & _
        "WHERE (((""" & DLookup("[Credit]", "tblReferee Credit Rates", "[Level] = """ & [tblAssignments].[Level] & """ " & _
        "And [Position] = """ & [tblAssignments].[Position] & """") & """) Is Null))"
        Set rst1 = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    the strSQL string (except I put in """ & ... & """ around the DLookup in this try) came from the Row Source Property of a Combo Box which works fine. I am guessing the problem is in using the ampersand and quote marks correctly but I cannot get it to work.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,623
    One problem is the spaces in the table name in the DLookup. Enclose the name in []. Now what happens?

    A coding technique that can help debug long sql statement is to build the string in stages.

    strSQL = strSQL & part1
    strSQL = strSQL & part2
    etc.

    Also, set breakpoint to pause code and view the compiled string. Combine this with Debug.Print strSQL to easily read the string in VBA Immediate window. Is it correct?
    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
    szucker1 is offline Novice
    Windows XP Access 2000
    Join Date
    Jun 2011
    Posts
    18
    June7,

    Thank you so much for your reply. Your coding technique was instrumental in helping me solve the problem. It was, as I thought, in the ampersands and quotes. I was treating the DLookup function, as well as, field names in the function as a variants by ending my string and then using ampersands to add them to the string. That was all wrong as I needed to just treat it all as characters and add extra quotes so the resultant string looked like what was in the Row Source Property. So the part of the string which was WRONG was:

    Code:
     
        "WHERE (((""" & DLookup("[Credit]", "tblReferee Credit Rates", "[Level] = """ & [tblAssignments].[Level] & """ " & _
        "And [Position] = """ & [tblAssignments].[Position] & """") & """) Is Null))"
    and after being CORRECTED is:

    Code:
     
        "WHERE ((DLookup(""[Credit]"", ""[tblReferee Credit Rates]"", ""[Level] = """""" & [tblAssignments]![Level] & """""" " & _
        "And [Position] = """""" & [tblAssignments]![Position] & """""""") Is Null))"
    The the table name with spaces that was not in [] was not a problem because the table name was already in quotes for the domain part of the DLookup function but I went ahead added the brackets just to be consistant.

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

Similar Threads

  1. OpenRecordset vs. Query
    By crispy-bacon in forum Programming
    Replies: 7
    Last Post: 07-04-2011, 09:52 AM
  2. DLookUp Help
    By ThaGreenMoose in forum Forms
    Replies: 10
    Last Post: 06-15-2011, 12:08 PM
  3. Dlookup help
    By jcaptchaos2 in forum Access
    Replies: 17
    Last Post: 04-21-2011, 01:33 PM
  4. DLookup
    By neil45156 in forum Forms
    Replies: 1
    Last Post: 04-11-2011, 11:31 AM
  5. DLookup Help
    By Alex Motilal in forum Queries
    Replies: 3
    Last Post: 01-17-2011, 02:01 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