我所有的变量都可以工作,我可以使每个变量的值显示在文本框中,并且代码可以平稳运行。但是,当我尝试更新table1时,DoCmd.RunSQL行出错了。也许是因为将值添加到ID = Y会使Y不再为真吗?
Y = DMax("[ID]", "Table1", "Len(Due_date) = 0")
X = DMin("[ID]", "Table1")
A = DMin("[ID]", "Table1", "Len(Due_date) > 0")
B = DLookup("[Duration]", "Table1", "[ID] = " & Y)
C = DLookup("[Due_date]", "Table1", "[ID] = " & A)
E = DateAdd("d", -B, C)
Dim SQL As String
SQL = "UPDATE Table1 " & _
"SET Table1.Due_date = '" & E & "'" & _
"WHERE Table1.ID = '" & Y & "'"
DoCmd.RunSQL SQL
答案 0 :(得分:1)
我猜是由于您的日期E
使用SQL字符串中的区域设置进行了格式化而引起的。
您可以使用BuildCriteria方法创建fieldname ComparisonOperator CompareExpression
形式的SQL字符串,例如Due_date=#10/27/2018#
。尽管CStr
使用区域设置将值转换为字符串,但BuildCriteria会将该区域字符串转换为正确格式的字符串以供SQL命令使用,就像在使用区域格式输入条件时查询设计器所做的一样。您的代码可能如下所示(未使用变量X
)
Y = DMax("ID", "Table1", BuildCriteria("Due_date", dbDate, "Null"))
A = DMin("ID", "Table1", BuildCriteria("Due_date", dbDate, "Not Null"))
B = DLookup("Duration", "Table1", BuildCriteria("ID", dbLong, CStr(Y)))
C = DLookup("Due_date", "Table1", BuildCriteria("ID", dbLong, CStr(A)))
E = DateAdd("d", -B, C)
Dim SQL As String
SQL = "UPDATE Table1 " & _
"SET " & BuildCriteria("Due_date", dbDate, CStr(E)) & " " & _
"WHERE " & BuildCriteria("ID", dbLong, CStr(Y))
DoCmd.RunSQL SQL