【问题标题】:Update a set of data values using a textbox in a form使用表单中的文本框更新一组数据值
【发布时间】:2021-09-02 20:46:58
【问题描述】:
我正在使用“多项”表单来显示来自查询的记录。
“日期”字段/列为空。
在表单标题上,我使用了一个文本框,用户必须在其中选择或输入日期(默认为日历)。
在表单标题上还有一个按钮,我想用在 TextBox 中输入的那个来更新所有日期记录。
我尚未测试的一种方法是让按钮运行更新查询:
UPDATE Table1 SET MyDate = [Forms]![Form1]![MyTextBox]
对理想方法有什么建议吗?
【问题讨论】:
标签:
sql
vba
database
ms-access
【解决方案1】:
由于这是 Access,请使用已保存所有记录的 RecordsetClone:
Private Sub YourButton_Click()
Dim Records As DAO.Recordset
Dim NewDate As Date
Set Records = Me.RecordsetClone
If IsNull(Me!MyTextBox.Value) Then
' Nothing to do.
ElseIf Recordset.RecordCount > 0 Then
Records.MoveFirst
NewDate = Me!MyTextBox.Value
With Records
.MoveFirst
While Not .EOF
If DateDiff("d", !MyDate.Value, NewDate) <> 0 Then
' Edit the record.
.Edit
!MyDate.Value = NewDate
.Update
End If
.MoveNext
Wend
.Close
End With
End If
End Sub