Results 1 to 5 of 5
  1. #1
    jslings is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Apr 2019
    Posts
    2

    Question VBA Update not working for null values

    I have VBA behind a membership management form that allows a user to assign a contactID to multiple parcel records in a table at once. When the user updates the contactID for one of the parcel record using the form, a pop up asks if the update should be applied to all remaining parcel records. If the user clicks yes, then the contactID is updated for all parcel records. When the update involves adding or changing the contactID to a number, the code works fine. When the update is to remove a contactID (i.e. clear it out to set as null), then a syntax error pops up. I'm not sure how to edit the VBA to update the contactIDs for all parcels to null in these cases.

    current vba:



    CurrentDb.Execute "UPDATE tbl_Parcels SET tbl_Parcels.ContactID = " & Me.ContactID & " WHERE MemberID = " & Me.MemberID


    The issue occurs when me.ContactID = null

    The table does allow nulls in the field. We tried changing the = to "is" and that did not solve the problem.

    Thanks for your help!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    CurrentDb.Execute "UPDATE tbl_Parcels SET tbl_Parcels.ContactID = " & Nz(Me.ContactID, "Null") & " WHERE MemberID = " & Me.MemberID
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Well i was going to suggest:
    Code:
     If Nz(Me.ContactID, 0) <> 0 Then        
             CurrentDb.Execute "UPDATE Members SET Members.ContactID = " & Me.ContactID & " WHERE MemberID = " & Me.MemberID
        Else
            CurrentDb.Execute "UPDATE Members SET Members.ContactID = Null WHERE MemberID = " & Me.MemberID
        End If
    but Paul's solution which I've just tested works and is simpler
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    jslings is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Apr 2019
    Posts
    2
    Nz(Me.ContactID, "Null") did the trick! Simple and effective. Thanks for your help!

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 0
    Last Post: 07-30-2017, 08:27 AM
  2. Replies: 5
    Last Post: 08-31-2016, 06:04 PM
  3. Working with queries and null values
    By guidout in forum Queries
    Replies: 6
    Last Post: 03-26-2015, 05:53 PM
  4. Finding the Max Date and Null Values if Null
    By SpdRacerX in forum Queries
    Replies: 1
    Last Post: 02-03-2012, 06:29 AM
  5. Update field1 if Null, if Not Null update field2
    By mfirestorm in forum Queries
    Replies: 2
    Last Post: 12-02-2011, 09:51 AM

Tags for this Thread

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