Results 1 to 4 of 4
  1. #1
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52

    DLookup return value

    I am currently running a script that uses DLookup. If the item is found, DLookup will return a number (used as currency) and calculations will be performed on this item. As I understand it, if DLookup does not return a value, it will return as NULL. Is there a way to check if it returns NULL?



    Originally, my code looked like this, but it returned with a mismatch type error.
    Code:
        Dim Price As Currency
    
        Price = DLookup("[CostUnit]", "qryAvgIngredientCost", "[Item_ID] = " & CurrentItem)
        If Price Is Null Then
            MsgBox ("Item not found. Cost calculation will be incomplete.")
            Else
                ' Calculations
        End If
    Here, I tried with a variant type to handle "Is Null" and it returned with a 424 error.
    Code:
        Dim Price As Currency
        Dim LookTest as Variant
    
        LookTest = DLookup("[CostUnit]", "qryAvgIngredientCost", "[Item_ID] = " & CurrentItem)
        If LookTest Is Null Then
            MsgBox ("Item not found. Cost calculation will be incomplete.")
            Else
                Price = LookTest
                ' Calculations
        End If

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    You can use the NZ() function. If a Null is returned, you can have it default to any value you want eg 0.

    for example
    Dim looktest as currency

    LookTest = NZ(DLookup("[CostUnit]", "qryAvgIngredientCost", "[Item_ID] = " & CurrentItem),0)

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Currency type fields cannot handle Nulls. I think that only Variants can contain nulls. One way to deal with Nulls is to use the NZ() function (null to zero). In VBA, you should use the IsNull() function to check for Nulls.

    Code:
        Dim Price As Currency
    
        Price = Nz(DLookup("[CostUnit]", "qryAvgIngredientCost", "[Item_ID] = " & CurrentItem), 0)
        '    If IsNull(Price) Then
        If Price = 0 Then
            MsgBox ("Item not found. Cost calculation will be incomplete.")
        Else
            ' Calculations
        End If
    or
    Code:
        Dim Price As Currency
        Dim LookTest As Variant
    
        LookTest = DLookup("[CostUnit]", "qryAvgIngredientCost", "[Item_ID] = " & CurrentItem)
        If IsNull(LookTest) Then
            MsgBox ("Item not found. Cost calculation will be incomplete.")
        Else
            Price = LookTest
            ' Calculations
        End If
    
    End Sub

  4. #4
    faythe1215 is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    52
    Perfect! Thanks!

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

Similar Threads

  1. Replies: 9
    Last Post: 08-19-2014, 12:41 PM
  2. Return DLookup result as date?
    By kman42 in forum Access
    Replies: 8
    Last Post: 04-22-2011, 11:35 AM
  3. Specify return value
    By Desstro in forum Queries
    Replies: 2
    Last Post: 06-19-2010, 02:12 AM
  4. Dlookup to match two criteria and return value
    By randolphoralph in forum Programming
    Replies: 20
    Last Post: 05-20-2010, 12:27 PM
  5. Return value of row above
    By marimar1097 in forum Access
    Replies: 7
    Last Post: 10-20-2009, 08:06 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