如何使用 VBA 在 Access 中创建带有时间和日期戳的评论框
How to create a comment box with time and date stamp in Access using VBA
对 VBA 完全陌生,需要有关详细说明的帮助(对我来说是虚拟版本)。
我有一个 table 包含各种列和以下列,具体来说:
审稿人评论
新评论
我创建了一个包含这两个字段的表单,需要创建一个 Append Comment 按钮,将文本从 NewComment 字段移动并将其附加到 ReviewerComment 字段,并且 time/date 在添加评论时标记它们。我将此按钮命名为 cmdAppendComment。
我见过别人 post 的东西,我试过了,但由于我对此完全陌生,我知道我把它搞砸了。非常感谢任何帮助。
这就是 VBA 代码现在的样子:
Private Sub cmdAppendComment_Click()
If (IsNull(NewComment.Value)) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(ReviewerComments.Value)) Then
ReviewerComments.Value = NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
ReviewerComments.Value = ReviewerComments.Value & _
vbNewLine & vbNewLine & _
NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
NewComment.Value = ""
End Sub
我对你的代码有一些建议:
1. 不检查文本框是否为空,而是检查文本框有多少个字符。你应该总是那样做,否则你往往会出错。
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
在这里检查文本框中字符串的长度。你需要附加“”,否则你往往会得到空错误或类似的东西。
2. 您忘记正确引用表单中的对象。您有自己的表格,并在该表格中放置文本框和 VBA 代码。您可以使用 "Me.[FormObjects]".
引用所有对象
编译器抱怨 "NewComment.Value" 或 "ReviewerComment.Value" 没有初始化,换句话说,没有维度。有了正确的参考,这应该停止。
Private Sub cmdAppendComment_Click()
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(Me.ReviewerComments.Value)) Then
Me.ReviewerComments.Value = Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
Me.ReviewerComments.Value = Me.ReviewerComments.Value & _
vbNewLine & vbNewLine & _
Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
Me.NewComment.Value = ""
End Sub
对 VBA 完全陌生,需要有关详细说明的帮助(对我来说是虚拟版本)。
我有一个 table 包含各种列和以下列,具体来说:
审稿人评论 新评论
我创建了一个包含这两个字段的表单,需要创建一个 Append Comment 按钮,将文本从 NewComment 字段移动并将其附加到 ReviewerComment 字段,并且 time/date 在添加评论时标记它们。我将此按钮命名为 cmdAppendComment。
我见过别人 post 的东西,我试过了,但由于我对此完全陌生,我知道我把它搞砸了。非常感谢任何帮助。
这就是 VBA 代码现在的样子:
Private Sub cmdAppendComment_Click()
If (IsNull(NewComment.Value)) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(ReviewerComments.Value)) Then
ReviewerComments.Value = NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
ReviewerComments.Value = ReviewerComments.Value & _
vbNewLine & vbNewLine & _
NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
NewComment.Value = ""
End Sub
我对你的代码有一些建议:
1. 不检查文本框是否为空,而是检查文本框有多少个字符。你应该总是那样做,否则你往往会出错。
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
在这里检查文本框中字符串的长度。你需要附加“”,否则你往往会得到空错误或类似的东西。
2. 您忘记正确引用表单中的对象。您有自己的表格,并在该表格中放置文本框和 VBA 代码。您可以使用 "Me.[FormObjects]".
引用所有对象编译器抱怨 "NewComment.Value" 或 "ReviewerComment.Value" 没有初始化,换句话说,没有维度。有了正确的参考,这应该停止。
Private Sub cmdAppendComment_Click()
If (len(Me.NewComment.Value & "") > 0) Then
MsgBox ("Please provide a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
If (IsNull(Me.ReviewerComments.Value)) Then
Me.ReviewerComments.Value = Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
Else
Me.ReviewerComments.Value = Me.ReviewerComments.Value & _
vbNewLine & vbNewLine & _
Me.NewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
End If
Me.NewComment.Value = ""
End Sub