使用 strUpdate 从表单字段访问 2010 更新 Table

Access 2010 Update Table from Form Field using strUpdate

我有一个表单,用户可以通过该表单选择当前财政月末日期。

当用户单击复选标记时,我希望该日期通过以下代码更新 table Tbl_Calendar_SetBucketDates 中的字段 CurrentFiscalMonthEnd: 选项比较数据库

Private Sub Image_BucketSelector_Click()
Dim db As DAO.Database
Dim strUpdate As String

'FirstBucketDate = Forms!Frm_SetBuckets!CurrentFiscalMonthEndDate_Selected
'FirstBucketDate = "05/29/2015"
FirstBucketDate = Date

  message = MsgBox("Did you set the Current Fiscal Month-End Date Selector?" & vbCrLf, vbYesNo, "Are you sure?")
  If message = vbYes Then
        DoCmd.SetWarnings False


'strUpdate = "Update Tbl_Calendar_SetBucketsDates SET [CurrentFiscalMonthEnd] = Forms!Frm_SetBuckets!CurrentFiscalMonthEndDate_Selected"
strUpdate = "Update Tbl_Calendar_SetBucketsDates SET [CurrentFiscalMonthEnd] = FirstBucketDate"
Debug.Print strUpdate
Set db = CurrentDb
Debug.Print strUpdate
db.Execute strUpdate, dbFailOnError
Debug.Print db.RecordsAffected & " rows updated"
Set db = Nothing
DoCmd.SetWarnings True
End If

If message = vbNo Then Exit Sub
 MsgBox "You have successfully set the bucket date!"

End Sub

根据取消选中的代码,我会收到大量错误。 我应该使用其他代码来更新该字段还是进行简单的更改。非常感谢您敏锐的眼光。 谢谢

更改此行:

strUpdate = "Update Tbl_Calendar_SetBucketsDates SET [CurrentFiscalMonthEnd] = FirstBucketDate"

收件人:

strUpdate = "Update Tbl_Calendar_SetBucketsDates SET [CurrentFiscalMonthEnd] = #" & Format(FirstBucketDate,"mm\/dd\/yyyy") & "#;"

这将在 Access SQL 中正确设置您的日期格式。

此外,请确保在此字符串中放置适当的 WHERE 子句,否则最终可能会更新比预期更多的行。