如何在Access 2007中使用Visual Basic代码更新单元格

时间:2014-01-27 04:19:57

标签: ms-access ms-access-2007 access-vba

所以我的项目是根据会议开始日期计算各种项目的截止日期列表。我一直试图找出如何使用Access 2007(雇主软件)根据会议开始日期的变化生成截止日期。

我的失败包括:     写冲突消息< - 试图摆脱这个     不承认变量     尝试了查询,但无法让查询显示在表单

ConferenceStartDate是包含Form上用户输入的列,以及我希望基于所有其他日期的变量。我让Form使用“Before Update”子程序进行更改。

Private Sub ConferenceStartDate_BeforeUpdate(Cancel As Integer)

  ' Concept is to enter Volume and Conference Date Start/End and have it calculate the rest of the dates
  ' Then compare to current date and create a report on over due, next due, etc
  ' Then create emails based on templates for next data

  Dim rstNameList As DAO.Recordset
  Set rstNameList = Application.CurrentDb.OpenRecordset("Table1", dbOpenDynaset)
  Dim startDate As Date
  Dim endDate As Date
  Dim recordNumber As Integer
  Dim stringRecordNumber As String
  Dim stringSQL As String

  ' *** Second Attempt
  ' Gives Write Conflict message.  Weird, if you say Update, moving to the next record does not update, but saying No Update actually updates the record.
  recordNumber = [ID]
  stringRecordNumber = "ID=" & CStr(recordNumber)

  ' Gets the start date of the conference
  startDate = [ConferenceStartDate]

 ' Add seven "d"ays tp startDate
 endDate = DateAdd("d", 7, startDate)


 ' rstNameList.FindFirst stringRecordNumber
 ' rstNameList.Edit
    'rstNameList!VolumeName = "MC-130"
 '     rstNameList!ConferenceStartDate = startDate
 '     rstNameList!ConferenceEndDate = endDate
 ' rstNameList.Update
 ' rstNameList.Close

 ' Gets rid of the Write Conflict error message
 ' Command doesnt work
 ' If Forms("Table1").Dirty Then Forms("Table1").Dirty = False

 ' *** First Attempt
 'Works to add
 'Set db = CurrentDb
 'Set rs = db.OpenRecordset("Table1")
 'rs.AddNew
  'rs("ID") = 5
  'rs("VolumeName") = "KC-130"
  'rs("ConferenceStartDate") = "1/1/1111"
  'rs("ConferenceEndDate") = "1/2/1212"
 'rs.Update
 'rs.Close

 ' *** Third Attempt
 ' Doesn't actually update
 ' stringSQL = "UPDATE Table1 SET [ConferenceEndDate] = #" & CStr(endDate) & "# WHERE " & stringRecordNumber

 ' DoCmd.RunSQL stringSQL

 End Sub

任何人都知道如何没有写冲突消息,并且日期根据单个日期的表格输入更新?

感谢名单!

1 个答案:

答案 0 :(得分:0)

如果您的表单绑定到表,那么您不应该尝试使用SQL语句来更新表单中的当前记录。如您所见,这可能导致写入冲突,因为两个“进程”正在尝试同时更新同一记录。

相反,您应该为要更新的字段创建绑定控件(必要时隐藏它们),然后更新这些控件的值。