Results 1 to 4 of 4
  1. #1
    graffiti is offline Newest Newbie ever
    Windows XP Access 2003
    Join Date
    Nov 2011
    Location
    Worthing, south coast of the UK
    Posts
    21

    Newbie. Cannot understand why I'm getting "Invalid use of null".

    Hi.



    The below code returns an error "invalid use of null". I don't see the problem, can you advise please?


    Code:
     
    tblDBUsers
    [ID]              autonumber
    [UserName]    text    50 characters
     
    Dim varUserID as long
     
    Watch :   :     varUserName : "Michael" : Variant/String : Form_frmLogIn.btnLogInFormClose_Click
     
    'Return the ID number of the matching UserName 
    varUserID = DLookup("[ID]", "tblDBUsers", "[UserName] = '" & varUserName & "'")
    I know it will be simple. Thanks.
    Last edited by graffiti; 12-20-2011 at 08:42 AM.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, the code would error if the DLookup couldn't find a match, as it would return Null, and a Long variable can't accept a Null. Is there a record with that user name, and if so does it have a non-null ID?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Handle possibility of null. One way is Nz function.
    varUserID = Nz(DLookup("[ID]", "tblDBUsers", "[UserName] = '" & varUserName & "'"),0)

    Searches on names is unreliable. What if there is more than one Michael?
    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.

  4. #4
    graffiti is offline Newest Newbie ever
    Windows XP Access 2003
    Join Date
    Nov 2011
    Location
    Worthing, south coast of the UK
    Posts
    21
    Hi.

    Thanks for the replies, I knew it was obvious! I'm trying to teach myself from online snippets etc in the odd 20 minutes here and there, so my knowledge is very patchy. So far.

    The user name was *not* on the list, deliberately. I want to be able to catch a non existant entry but I didn't know enough to be able to. I now know more, thanks.

    Mike.

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

Similar Threads

  1. Replies: 13
    Last Post: 12-05-2011, 05:10 AM
  2. Replies: 1
    Last Post: 10-15-2011, 01:12 PM
  3. StrComp causing "Invalid use of Null" error
    By sephiroth2906 in forum Programming
    Replies: 5
    Last Post: 09-15-2011, 07:06 PM
  4. Update query from form "invalid use of null"
    By Lady_Jane in forum Programming
    Replies: 4
    Last Post: 08-16-2011, 01:37 PM
  5. "Invalid Database Object Reference"
    By jgelpi16 in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 06:39 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