我的表中有一些数字和短日期格式的控件,日期控件也被屏蔽为mm / dd / yyyy。加载到表单中的一些字段在原始表中是空白的,因此在执行sql时,我实际上是在评估错误的东西,无论是检查''还是Null。 as'作为日期编号的文本失败,字段实际上不是空白。
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
错误3075查询表达式中与函数一起使用的参数数量错误 'IIF(IsNull(),0,'
我也尝试从另一个网站
进入该字段本身 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
相同错误3075'IIF(IsNull(),0,[ProposedHrs]'
如果我使用IIF(IsNull方法而不是='',***也会失败
我没有粘贴日期失败的例子,但是它是相同的想法,不是空但是空白,但似乎不能再次更新回空白甚至可能跳过?
提前感谢任何人。
尝试Erik Von Asmuth代码的Update-1< - 谢谢btw!
Set qdf = db.CreateQueryDef("", & _
"UPDATE [tblDetails] SET " & _
"[Proposed] = @Proposed, " & _
"[Multi] = @Multi, " & _
"[Rational] = @Rational " & _
"WHERE [RNumber] = @RNumber")
这部分全是红色,第一个是“&”关闭通知窗口后突出显示 编译错误: 预期:表达
更新-2:我将更新移到了第一行,它似乎正在运行。设置qdf = db.CreateQueryDef(“”,“UPDATE [tblDetails] SET”& _
我将在下一个日期字段中尝试此方法。
更新-3:尝试使用99/99/0000蒙版的文本框进行相同的参数化时; 0; _我收到的是在集合中找不到的项目?我已多次检查拼写,一切似乎都没问题。我尝试了以下三种格式,因此设置参数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)
更新-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
请注意文本框txtDateRcvd在文本框的属性中设置了输入掩码99/99/0000; 0; _。
答案 0 :(得分:0)
您应该考虑空字符串,并在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 & "'"
或者更好的是,正确执行此操作并参数化整个更新,以便您无法获得这些错误或SQL注入。
如何正确执行此操作的示例:
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