简体   繁体   English

MS-Access 更新 SQL 不是空而是空白(!日期和数字字段!)

[英]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.我的表格中有一些数字和短日期格式的控件,日期控件也被屏蔽为 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.加载到表单中的一些字段在原始表中是空白的,因此在执行 sql 时,我实际上是在评估错误的事情,无论我检查的是 '' 还是 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,'错误 3075 在查询表达式 '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]'同样的错误 3075 'IIF(IsNull(),0,[ProposedHrs]'

***also fails if I use the IIF(IsNull method as opposed to the ='' ***如果我使用 IIF(IsNull 方法而不是 =''

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!尝试 Erik Von Asmuth 代码的 Update-1 <--谢谢顺便说一句!

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.更新 2:我将更新移到第一行,它似乎正在工作。 Set qdf = db.CreateQueryDef("", "UPDATE [tblDetails] 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?更新 3:当使用 99/99/0000;0;_ 屏蔽的文本框尝试相同的参数化时,我收到的项目在集合中找不到? 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?我尝试了以下三种格式,因此设置了参数 DateRcvd,任何人都可以评论这对于带有日期的文本框是否正确?

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:更新 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.请注意文本框 txtDateRcvd 在文本框的属性中设置了输入掩码 99/99/0000;0;_。

You should account for empty strings, and do that IIF statement in VBA instead of SQL:您应该考虑空字符串,并在 VBA 而不是 SQL 中执行该IIF语句:

        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.或者更好的是,正确执行并参数化整个更新,这样您就不会遇到此类错误或 SQL 注入。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM