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

时间:2015-05-15 20:13:52

标签: access-vba ms-access-2010

我有一个表单,用户通过该表单选择当前的会计月末日期。 Frm_SetBuckets

当用户点击复选标记时,我希望该日期通过以下代码更新表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

我遇到了很多错误,具体取决于我解开的代码。 我应该使用其他一些代码更新字段还是有一个简单的更改。非常感谢您的敏锐眼光。 感谢

1 个答案:

答案 0 :(得分:1)

更改此行:

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

致:

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

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

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