我有一个表单,其中包含一个名为StartDate的未绑定短日期字段和一个单击按钮,用于启动VBA代码,如下所示,我得到StartDate的“输入参数消息”。 StockTakeDate也是一个短期日期。任何有关摆脱信息的建议都会受到欢迎。
Private Sub cmdInventory_Click()
strMsg = "Enter StartDate as Today's date or later"
If Me.[StartDate] < Date Then
MsgBox strMsg, ExitSub
Else
Dim strSQL
strSQL = " UPDATE [Inventory] " _
& " SET [Inventory].[InStock] = [Inventory].[StockTake] " _
& " WHERE ([Inventory].[StockTakeDate] >= Me.[StartDate]) "
DoCmd.RunSQL strSQL
End Sub
使用Me.SomeField
时不能使用DoCmd.RunSQL
。相反,使用Forms!SomeForm!SomeField
:
Dim strSQL
strSQL = " UPDATE [Inventory] " _
& " SET [Inventory].[InStock] = [Inventory].[StockTake] " _
& " WHERE ([Inventory].[StockTakeDate] >= Forms!MyFormName![StartDate]) "
DoCmd.RunSQL strSQL
连接格式化为字符串表达式的参数值(StartDate):
strSQL = " UPDATE [Inventory] " _
& " SET [Inventory].[InStock] = [Inventory].[StockTake] " _
& " WHERE ([Inventory].[StockTakeDate] >= #" & Format(Me![StartDate].Value, "yyyy\/mm\/dd") & "#"
DoCmd.RunSQL strSQL