Results 1 to 10 of 10
  1. #1
    rdougherty is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107

    If isnull test failing for dlookup returning empty value

    Hello, please help me understand if the following would be seen as a null value, zero length, etc. I am using dlookup in an if, else statement, and my empty cells seem to be passing a not null value, because the code is running that is in the else clause...

    I have a table with a single row where values exist for the purpose of calling them as variables on a form, and then set to null when not needed. (I do not wish to delete the record entirely)

    I need to do a logic test to use this data if it is not null, else do nothing.
    Code:
    If isnull(DLookup("field1", tablename) then
    'Nothing
    Else
    Var = DLookup("field1", tablename)
    End if
    Even when the values being looked up by dlookup are set to null prior, this if statement is doimg the thing in the else clause...



    Question is whether the dlookup is seeing a null value is still seen as "something" in this scenario because it found a row meeting its criteria (no criteria statement = return the only value in the table since there can only be one row).

    See notes from microsoft:
    If criteria is omitted, the DLookup function evaluates expr against the entire domain.

    And...

    The DLookup function returns a single field value based on the information specified in criteria. Although criteria is an optional argument, if you don't supply a value for criteria, the DLookup function returns a random value in the domain.

    And finally...

    If no record satisfies criteria or if domain contains no records, the DLookup function returns a Null.
    That last bit seems to strongly suggest that if dlookup finds anything meeting its criteria, that it will not pass an isnull test...

    So does it return a zero length here, or what?

    How do I test for that so that I only run my code when there is a data value present in the dlookup field?

    Edit: I just read somewhere about vbNullString and Is Nothing

    Would either of those do the trick for my use case?

    Thanks!

  2. #2
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    you save the result first of DLookup then test if it is null.
    also you need to Enclose your table/query name to quote string (as in the example).
    Note you need to declare the variable as Variant.

    Code:
    Dim rslt As Variant
    rslt = DLookup("field1", "tablename")
    
    If Not IsNull(rslt) Then
    ' you use the value of rslt here
    ' example
    'Me!txtBox = rslt
    End If

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Nothing wrong with combining IsNull and DlookUp() ?
    Code:
    ? isnull(dlookup("dayno","tblday","dayno=32"))
    True
    Perhaps you have a ZLS and not Null ?
    Code:
    ? dlookup("dayno","tblday","dayno=32") & "" = ""
    True
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    I always use the Nz() function (the default for text fields is ZLS or ""):
    https://support.microsoft.com/en-us/...a-7fd9f4c69b6c
    Code:
    If Nz(DLookup("field1", "tblYourTable"))="" Then  'If IsNull(DLookup("field1", tablename)) then
         'Nothing
    Else
         Var = DLookup("field1", "tblYourTable")
    End if
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I don't allow ZLS in text fields. The goal is to not have to deal with possible ZLS. However, doesn't hurt to code for it. Concatenate "" or use Nz(), either will accomplish same.

    I would use a variable instead of repeating the DLookup().
    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.

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    If Var is a Variant then I'd omit Else's when they're not really necessary

    Code:
    Var = DLookup("fieldname", "tablename") 'lookup returns Null if not found
    'then some sort of examination of Var, such as
    If Not IsNull(Var) Then 
        'do something, else the whole If block is bypassed.
    End If
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    rdougherty is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107
    Thank you all. I will try some of your suggestions as soon as I can get more time with this database. My job is mostly afk, so dev time is severely limited!

  8. #8
    rdougherty is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Dec 2016
    Location
    Michigan
    Posts
    107
    After reading your responses more carefully, and reading up on what the nz() function is used for, I think perhaps my problem was misunderstood...

    The trouble I have is not that I am getting a null value when I don't desire one, but rather that the dlookup may not be able to return a null value in my situation and then be evaluated by isnull() because the recordset it is returning is not a null set (contains 1 record of all null fields), even though the field I am asking to evaluate with isnull() is a null value. So, is the dlookup already passing a not null value in this case to the isnull() to evaluate (because it found a record), and therefore isnull() may not be the optimal way to evaluate the result of the dlookup?

    I only want an "apply batch data entry defaults" button to populate a field with those defaults if the user actually entered a default value for the field as part of their entering data in a batch... if they only want to default 1 of 10 fields that all of their records will have in common, and manually enter the other 9, for instance I want to update the new record's empty fields only if the batch default was set for that field, and contains a value.

    It almost seems like I need the opposite of a nz() that will force a zls to null so that it can be evaluated by isnull???

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Huh?

    First of all, domain aggregate functions don't return a recordset - it returns a single value. If the function has criteria that does not find any match, it returns Null (except for DCount which will return 0). Unless you are feeding into the criteria an invalid parameter for the expression, which will error. Hard to say since you still haven't provided the DLookup() expression nor any data.

    As I stated, issues with empty string are why I don't allow in table.

    You need to provide sample data, desired result, and enough code so we can debug this. If you want to provide file, follow instructions at bottom of my post.
    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.

  10. #10
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    dlookup may not be able to return a null value
    To back up June7 I already stated that if a value isn't returned by that function it defaults to Null, so that's a non-starter.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 5
    Last Post: 03-11-2024, 09:38 PM
  2. Replies: 9
    Last Post: 10-06-2014, 02:02 PM
  3. isnull and not isnull
    By webisti in forum Programming
    Replies: 11
    Last Post: 03-04-2014, 08:44 AM
  4. Test String test besed on table data
    By igourine in forum Programming
    Replies: 3
    Last Post: 12-01-2013, 06:16 AM
  5. Keying in Test answers to Access DB from Written Test
    By CityOfKalamazoo in forum Access
    Replies: 3
    Last Post: 03-01-2010, 08:58 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