Results 1 to 4 of 4
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097

    Table name substitution in DLookup

    I can't seem to hit on the appropriate syntax wherein I can substitute a string variable containing the name of a table. E.g., strTBLName = tblRegister. I've tried the assortment of statements below without success. It would be hard to believe, but maybe such substitutions aren't supported in the VBA language set?

    Code:
    curAcBal = DLookup("[CurBal]", strTBLName , "[Acctid] =" & AcID)
    
    curAcBal = DLookup("[CurBal]", "strTBLName", "[Acctid] =" & AcID)
    
    curAcBal = DLookup("[CurBal]", " & strTBLName & ", "[Acctid] =" & AcID)


  2. #2
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Maybe you need to wrap your string variable in quotes and probably brackets (I'd try single quotes first)
    DLookup("[CurBal]", & "'[" & strTBLName & "]'" , "[Acctid] =" & AcID)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    strTBLName was set to the wrong string name which obscured the real problem.

    As a string,
    curAcBal = DLookup("[CurBal]", strTBLName , "[Acctid] =" & AcID)

    works as expected.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    This syntax worked for me
    Code:
    curAcBal = DLookup("[CurBal]", strTBLName , "[Acctid] =" & AcID)
    Test Code
    Code:
    Sub Dlookuptest()
        Dim tmpString As String
        Dim strTBLName As String
    
        strTBLName = "MyData"
    
        ' DLookup returns a text string
        tmpString = DLookup("[competitor]", strTBLName, "[rank] =" & Me.rank)
        MsgBox tmpString
    End Sub

    late again!!

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

Similar Threads

  1. String Variable substitution for SetFocus
    By GraeagleBill in forum Programming
    Replies: 1
    Last Post: 08-23-2014, 07:08 PM
  2. Replies: 1
    Last Post: 03-22-2013, 03:33 AM
  3. Replies: 3
    Last Post: 10-20-2012, 11:52 AM
  4. Variable substitution problem.
    By bsc in forum Programming
    Replies: 2
    Last Post: 01-11-2012, 09:33 AM
  5. Replies: 1
    Last Post: 08-13-2011, 12:03 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