Dlookup and No Data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vasago
    New Member
    • Mar 2014
    • 46

    Dlookup and No Data

    I have a Dlookup Function that performs perfectly if their is data but when there isn't data I run into the runtime error. What is the best way to work around this so my users aren't getting a error.

    Code:
    [Punch ID] = Nz(DLookup("[Punch ID]", "[Copy of Employee Work Statistics1]", "Employee='" & Forms![Royal time Stamp]![Driver] & "' And [Date]=#" & Date & "#" & " And [Trip Sign Off]=0"), 0)
  • Vasago
    New Member
    • Mar 2014
    • 46

    #2
    Problem solved using error handling
    Code:
    On Error GoTo error_nodata
    [Punch ID] = Nz(DLookup("[Punch ID]", "[Copy of Employee Work Statistics1]", "Employee='" & Forms![Royal Time Stamp]![Driver] & "' And [Date]=#" & Date & "#" & " And [Trip Sign Off]=0"), 0)
    [Type of Work] = Nz(DLookup("[Type of Work]", "[Copy of Employee Work Statistics1]", "Employee='" & Forms![Royal Time Stamp]![Driver] & "' And [Date]=#" & Date & "#" & " And [Trip Sign Off]=0"), 0)
    [Trip_S_on] = Nz(DLookup("[Trip Sign On]", "[Copy of Employee Work Statistics1]", "Employee='" & Forms![Royal Time Stamp]![Driver] & "' And [Date]=#" & Date & "#" & " And [Trip Sign Off]=0"), 0)
    error_nodata:
    Exit Sub

    Comment

    • Seth Schrock
      Recognized Expert Specialist
      • Dec 2010
      • 2965

      #3
      What is the error number and description that you get?

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        Neither set of code, as posted, will ever run correctly and without error. Variables in VBA cannot take brackets ([]) and the syntax makes no sense for SQL.

        It's hard to help when the question doesn't even make sense. Such a question, even with code that makes sense, would need an error message as a bare minimum.

        Comment

        Working...