我需要更新其中pdate(日期字段)不在当前月份和年份的第1天和第20天之间的行.
我正在使用下面编写的代码,但它给出了一个错误,说"预期参数太少"1.我使用MS Access 2007作为数据库.
cn.Execute "update water set prel = (prel + (mmt * (tx / 100))) where pdate not between 1-" & Format$(Now, "MMM") & "-" & Format$(Now, "YYYY") & " and 20-" & Format$(Now, "MMM") & "-" & Format$(Now, "YYYY") & " "
Run Code Online (Sandbox Code Playgroud)
您的直接问题是缺少#date分隔符,正如@MicSim向您展示的那样.但是我建议你考虑一下WHERE子句的不同方法.
添加#分隔符后,您的WHERE子句与此类似(使用今天的日期).
Debug.Print "WHERE pdate not between #1-" & _
Format$(Now, "MMM") & "-" & Format$(Now, "YYYY") & _
"# and #20-" & Format$(Now, "MMM") & "-" & _
Format$(Now, "YYYY") & "#"
WHERE pdate not between #1-May-2011# and #20-May-2011#
Run Code Online (Sandbox Code Playgroud)
一个重要的问题是您的所有pdate值是否包含午夜作为时间组件.(日期/时间值总是包含一个时间组件.)这可能是重要的原因是本月应该发生什么样的pdate 5/20/2011 10:18:15 AM?您的WHERE子句将导致更新.但那个日期仍然是5月20日......那是你想要的吗?
我认为修改后的WHERE子句会产生意外后果的风险较小.
Debug.Print "WHERE pdate < " & _
Format(DateSerial(Year(Date), Month(Date), 1), "\#yyyy-mm-dd#\") & _
" OR pdate >= " & _
Format(DateSerial(Year(Date), Month(Date), 21), "\#yyyy-mm-dd#\")
WHERE pdate < #2011-05-01# OR pdate >= #2011-05-21#
Run Code Online (Sandbox Code Playgroud)
你的问题被标记为vb6.DateSerial(),Year(),Month(),Date()和Format()函数都应该可以从数据库引擎的沙箱模式中获得.(请参阅Microsoft关于沙箱模式功能的页面).
编辑:感谢@Brian Camire的建议.
Debug.Print "WHERE pdate < " & _
"DateSerial(Year(Date), Month(Date), 1)" & _
" OR pdate >= " & _
"DateSerial(Year(Date), Month(Date), 21)"
Run Code Online (Sandbox Code Playgroud)