Search code examples
sqlvbadatems-accessnumber-formatting

MS-Access Update SQL Not Null But is Blank (! Date & Number Fields !)


I have a few controls that are number and short date format in my tables also the date controls are masked to mm/dd/yyyy. Some of the fields that are loaded into the form are blank from the original table and so when executing the sql I am essentially evaluating the wrong thing whether Im checking for '' or Null. as '' fails as text for date number and the fields are not actually blank.

        strSQL4 = "UPDATE [tblDetails] SET " & _
            "[Proposed] = IIF(IsNull(" & Forms!frmEdit.txtProposed.Value & "),0," & Forms!frmEdit.txtProposed.Value & "), " & _
            "[Multi] = IIF(IsNull(" & Forms!frmEdit.txtMulitplier.Value & "),0," & Forms!frmEdit.txtMulitplier.Value & "), " & _
            "[Rational] = '" & Forms!frmEdit.txtRational.Value & "' " & _
            " WHERE [RNumber] = '" & Forms!frmEdit.cmbUpdate.Value & "'"
            Debug.Print strSQL4
        dbs.Execute strSQL4

ERROR 3075 Wrong number of arguments used with function in query expression 'IIF(IsNull(),0,'

I also tried entering the field itself suggested from another site

        strSQL4 = "UPDATE [tblDetails] SET " & _
            "[Proposed] = IIF(" & Forms!frmEdit.txtProposed.Value & "='',[Proposed]," & Forms!frmEdit.txtProposed.Value & "), " & _
            " WHERE [RNumber] = '" & Forms!frmEdit.cmbUpdate.Value & "'"
            Debug.Print strSQL4
        dbs.Execute strSQL4

Same Error 3075 'IIF(IsNull(),0,[ProposedHrs]'

***also fails if I use the IIF(IsNull method as opposed to the =''

I did not paste an example of the dates failing, but is the same idea, not null but is blank, but cant seem to update back to blank again or even skip maybe?

Thanks to anyone in advance.

Update-1 from attempting Erik Von Asmuth code <--Thanks btw!

Set qdf = db.CreateQueryDef("", & _
            "UPDATE [tblDetails] SET " & _
            "[Proposed] = @Proposed, " & _
            "[Multi] = @Multi, " & _
            "[Rational] = @Rational " & _
            "WHERE [RNumber] = @RNumber")

This portion is all red and the first "&" is highlighted after closing the notification window Compile error: Expected: expression

Update-2: I moved the update to the first line and it seems to be working. Set qdf = db.CreateQueryDef("", "UPDATE [tblDetails] SET " & _

I am going to try this method with the dates fields next.

Update-3: when attempting the same parameterization with textbox's masked with 99/99/0000;0;_ I am receiving item not found in collection? I have checked the spelling several times and everything seems ok. I tried the following three formats so set parameter DateRcvd, can anyone comment if this is correct for a text box with dates?

qdf.Parameters("@DateRcvd") = IIf(Nz(Forms!frmEdit.txtDateRcvd.Value) = "", 0, Forms!frmEdit.txtDateRcvd.Value)
qdf.Parameters("@DateRcvd") = IIf(IsNull(Forms!frmEdit.txtDateRcvd.Value), 0, Forms!frmEdit.txtDateRcvd.Value)
qdf.Parameters("@DateRcvd") = IIf(Forms!frmEdit.txtDateRcvd.Value = "", 0, Forms!frmEdit.txtDateRcvd.Value)

Update-4:

Dim qdf2 As DAO.QueryDef
Set db = CurrentDb
Set qdf2 = db.CreateQueryDef("", "UPDATE [tblDetails] SET " & _
             "[DateReceived] = @DateRcvd " & _
             "WHERE [RNumber] = @RNumber")
             qdf.Parameters("@DateRcvd") = IIf(Nz(Forms!frmEdit.txtDateRcvd.Value) = "", 0, Forms!frmEdit.txtDateRcvd.Value)
             qdf.Parameters("@RNumber") = Forms!frmEdit.cmbUpdate.Value
             qdf2.Execute

Please Note text box txtDateRcvd has an Input Mask 99/99/0000;0;_ set within the properties of the textbox.


Solution

  • You should account for empty strings, and do that IIF statement in VBA instead of SQL:

            strSQL4 = "UPDATE [tblDetails] SET " & _
            "[Proposed] = " & IIF(Nz(Forms!frmEdit.txtProposed.Value) = "",0,  Forms!frmEdit.txtProposed.Value) & ", " & _
            "[Multi] = " & IIF(Nz(Forms!frmEdit.txtMulitplier.Value) = "",0, Forms!frmEdit.txtMulitplier.Value) & ", " & _
            "[Rational] = '" & Forms!frmEdit.txtRational.Value & "' " & _
            " WHERE [RNumber] = '" & Forms!frmEdit.cmbUpdate.Value & "'"
    

    Or better yet, do it properly and parameterize the whole update so you can't get these kind of errors or SQL injection.

    Example of how to do it properly:

    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Set db = CurrentDb
    Set qdf = db.CreateQueryDef("",  _
        "UPDATE [tblDetails] SET " & _
        "[Proposed] = @Proposed, " & _
        "[Multi] = @Multi, " & _
        "[Rational] = @Rational " & _
        "WHERE [RNumber] = @RNumber" 
    )
    qdf.Parameters("@Proposed") = IIF(Nz(Forms!frmEdit.txtProposed.Value) = "",0,  Forms!frmEdit.txtProposed.Value)
    qdf.Parameters("@Multi") = IIF(Nz(Forms!frmEdit.txtMulitplier.Value) = "",0, Forms!frmEdit.txtMulitplier.Value)
    qdf.Parameters("@Rational") = Forms!frmEdit.txtRational.Value
    qdf.Parameters("@RNumber") = Forms!frmEdit.cmbUpdate.Value
    qdf.Execute