Results 1 to 7 of 7
  1. #1
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18

    Update Statement with Null Value


    I have a query which builds a temp table to populating my form with. Not all the values in the table are completed as I may not have all the information. I have set an update statement, fired by a command button, to pass these values back to the proper table when the user makes changes.

    If my text fields are populated, the update statement works fine, however If I have no data in the text box, the update statement shows the field as NULL and fails. How do I get around this?

  2. #2
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18
    Here is the simple code I am using. All the fields in the tables are set to text, no default, and no requirements.
    Dim stSQL As String

    stSQL = "UPDATE tblItem " & _
    "SET tblItem.ItemNbr =" & Me.txtItemNbr & ", tblItem.BrennanItemNbr =" & Me.txtBrennanItemNbr & " " & _
    "WHERE tblItem.ID =" & Me.txtID

    DoCmd.RunSQL stSQL

  3. #3
    Micron is offline Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,361
    If those fields permit empty strings ("") you could try the NZ function to coerce Nulls to empty strings. I think that would work, but I'm getting rusty...
    Nz(Me.txtItemNbr,"")

    Note : As long as the nulls don't involve foreign keys. If you sql is really that simple, you could also try Is Null Or ...
    The error message would help if you're getting one.
    Last edited by Micron; 07-10-2017 at 05:15 PM. Reason: added info
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Why don't you just bind the form to "tblItem"?


    Quote Originally Posted by mcaliebe View Post
    Here is the simple code I am using. All the fields in the tables are set to text, <snip>
    Really? You are sure that the data types of ALL of the fields are TEXT? Even the PK field "ID"?

    Since text fields must be delimited, I wouldn't expect the SQL to execute at all.
    If the fields are TEXT datatypes, I would expect the SQL to look like (note the single quotes)
    Code:
    Dim stSQL As String
    
    stSQL = "UPDATE tblItem"
    stSQL = stSQL & " SET tblItem.ItemNbr = '" & Me.txtItemNbr & "',"
    stSQL = stSQL & " tblItem.BrennanItemNbr = '" & Me.txtBrennanItemNbr & "'" 
    stSQL = stSQL & " WHERE tblItem.ID = '" & Me.txtID & "';"
    
    '  DoCmd.RunSQL stSQL
    Currentdb.Execute stSQL, dbFailOnError    '<<-- I would use this

    Add Debug statement to see what the SQL statement looks like and post the result from the immediate window:
    Code:
    Dim stSQL As String
    
    stSQL = "UPDATE tblItem"
    stSQL = stSQL & " SET tblItem.ItemNbr = '" & Me.txtItemNbr & "',"
    stSQL = stSQL & " tblItem.BrennanItemNbr = '" & Me.txtBrennanItemNbr & "'" 
    stSQL = stSQL & " WHERE tblItem.ID = '" & Me.txtID & "';"
    
    Debug.Print stSQL
    
    Currentdb.Execute stSQL, dbFailOnError

  5. #5
    mcaliebe is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    18
    Hi Steve-

    You hit the nail on the head. I wasn't developing my code to pass a string. When it failed, and worked and failed again, was due to my use of a number as opposed to mixed text. Formatting with the correct parenthesis corrected my issue.

    As for the use of
    Currentdb.Execute stSQL, dbFailOnError

    I assume I would need an error trap in my module to make use of this statement. What do you recommend?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    A primer on error trapping:

    http://www.baldyweb.com/ErrorTrap.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Yes, error trapping is good. I use code like the link Paul provided.


    Here is a little more info on DoCmd.RunSQL vs CurrentDB.Execute (plus more)
    http://www.utteraccess.com/forum/lof.../t1953226.html

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

Similar Threads

  1. Help with code - need an if null statement
    By NikoTheBowHunter in forum Access
    Replies: 4
    Last Post: 06-08-2017, 08:07 AM
  2. Handling NULL with IIF statement
    By robbeh in forum Queries
    Replies: 7
    Last Post: 01-27-2016, 11:50 AM
  3. SQL Statement to find records with a Null date?
    By IncidentalProgrammer in forum Programming
    Replies: 4
    Last Post: 01-27-2015, 08:30 AM
  4. How to handle null in SQL statement
    By ultimateguy in forum Programming
    Replies: 21
    Last Post: 08-15-2014, 10:48 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