0

I am trying to update columns which contain Null values. I thought that the bellow code would work but I get syntax error for missing operator in query expression. I can't seems to figure it out. Any help?

Private sub dataUpdate_Click()
Dim SQL As String
On Error GoTo cancelledClicked

SQL = "UPDATE table1 " & _
"SET [Column1] = 1 WHERE [Column1] IS NULL " & _
"SET [Column2] = 0 WHERE [Column2] IS NULL; "

DoCmd.RunSQL SQL

exitDataUpdate:
Exit Sub

ignoreError:
MsgBox Err.Description
Exit Sub

cancelledClicked:
If Err.Number = 2501 Then GoTo exitDataUpdate
If Err.Number <> 2501 Then GoTo ignoreError

Resume Next

End sub
4
  • There is only one SET and one WHERE clause in an UPDATE statement. Each can contain multiple columns / conditions, though. -- But if you want to update two different data sets, you need two SQL statements. Commented Jun 28, 2018 at 11:42
  • Andre, thanks for the direction. I have 25 columns and curently, it seems like I need 25 SQL statements. Is that correct? Commented Jun 28, 2018 at 13:44
  • The important thing is the data set = the rows you want to update = the WHERE clause. If they are different, you need separate statements. Commented Jun 28, 2018 at 13:56
  • Sometimes the data set is complete with the required data in which case the data set should not be updated but in some cases the data set has a Null value in some fields it is therefore required that a Null is converted to 0. Commented Jun 28, 2018 at 14:11

1 Answer 1

2

Is this the logic you want?

UPDATE table1 
    SET [Column1] = NZ([Column1], 1),
        [Column2] = NZ([Column2], 0)
    WHERE [Column1] IS NULL OR [Column2] IS NULL

Your code has a syntax error.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, he doesn't have a space after the first null, so it ends up being ...WHERE [Column1] IS NULLSET [Column2]...
Gordon, that will update all Column1 fields where Column2 is Null.
@Zack . . . That would be the least of the syntax problems.
But coalesce is T-SQL. It should be Nz.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.