Results 1 to 11 of 11
  1. #1
    Gilligan is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    19

    Not figuring out dlookup (RunTime 2465)

    Ok, trying to do what I think is a simple dlookup to verify a UPC code is in the db before I do an append. If not then I'll add it but I'm not even there yet.

    Code:
    Private Sub theUPC_Enter()
        Dim A, B As Variant
        
        A = theUPC
        B = DLookup("[UPC]", "tblItems", [UPC] = A)
        
        If IsNull(B) = True Then
        MsgBox "Null!"
        Else
        MsgBox "Not Null!"
        End If
    End Sub
    My form has a text box for UPC... I'd like for this vba code to happen after the scan from my scanner (which ends with an ENTER command). That's not really happening but that's not really the point.

    When I run the code as is I get a Runtime error on the dlookup line. I THINK it is related to the field section.

    "tblItems" has several fields one is titled "UPC" so I thought this was the right way to do it.



    Basically, it checks to see if the UPC code is in the table or not. I also tried DCount with the same results.

    I'm just trying to learn how to use Access and I keep hitting brick walls.
    Last edited by Gilligan; 03-08-2011 at 09:54 AM. Reason: (solved)

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    All of the arguments for the Domain functions are strings: http://www.mvps.org/access/general/gen0018.htm

  3. #3
    Gilligan is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    19
    Ok... so I got it to work with this:

    Code:
    Private Sub theUPC_Enter()
        Dim A, B As Variant
        
        A = theUPC
        B = DLookup("UPC", "tblItems", "UPC = [theUPC]")
        
        If IsNull(B) = True Then
        MsgBox "Null!"
        Else
        MsgBox "Not Null!"
        End If
    End Sub
    Basically eliminating my variable. Which is fine here but I would like to better understand how I would use the variable instead of explicitly defining what I'm looking for in the criteria area. "UPC =[theUPC]"

  4. #4
    Sciencefair is offline Novice
    Windows XP Access 2003
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by Gilligan View Post
    Ok... so I got it to work with this:

    Which is fine here but I would like to better understand how I would use the variable instead of explicitly defining what I'm looking for in the criteria area. "UPC =[theUPC]"
    Use concatonation <sic> B = DLookup("UPC", "tblItems", "UPC = " & A)

  5. #5
    Gilligan is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    19
    That's giving me a "type mismatch" but I know that isn't exactly the code's fault.

    Not sure what "type" it wants... but at least I know the syntax.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    If it is a string then enclose the value with single quotes:
    B = DLookup("UPC", "tblItems", "UPC = '" & A & "'")

  7. #7
    Gilligan is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    19
    That worked... not sure I completely understand it... but sometimes that's just how things are.

    Thanks!

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The link I posted shows how to deal with the 3 different types of values.

  9. #9
    Gilligan is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    19
    You have extra double quotes... I'm sure that has to do with the concatenation but that isn't covered in that link you posted. Trust me, I tried the variations on that link.

    Thanks again!

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    It is the 2nd item:
    Code:
    Refering to a form control
    For numerical values:
            DLookup("FieldName", "TableName", "Criteria = " & forms!FormName!ControlName)
    For strings: (note the apostrophe before and after the value)
            DLookup("FieldName", "TableName", "Criteria = '" & forms!FormName!ControlName & "'")
    For dates:
            DLookup("FieldName", "TableName", "Criteria = #" & forms!FormName!ControlName & "#")

  11. #11
    Gilligan is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Mar 2011
    Posts
    19
    Ah ha! I guess it was the formatting, I think I looked at those single quotes as double quotes.

    My bad.

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

Similar Threads

  1. Need help figuring out stuff
    By Buakaw in forum Access
    Replies: 3
    Last Post: 01-31-2011, 02:59 AM
  2. Error in Runtime Only
    By drunkinmunki in forum Programming
    Replies: 7
    Last Post: 12-16-2010, 03:43 PM
  3. Runtime help! What am I to do?
    By shesgone in forum Access
    Replies: 2
    Last Post: 04-04-2010, 11:19 PM
  4. 2003 Runtime
    By miclar99 in forum Access
    Replies: 4
    Last Post: 01-12-2010, 05:30 PM
  5. Access runtime on CD?
    By Orabidoo in forum Access
    Replies: 1
    Last Post: 08-19-2009, 01:51 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