vba 如何在 Access 中创建带有时间和日期戳的评论框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19161364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:37:14  来源:igfitidea点击:

How to create a comment box with time and date stamp in Access

vbams-accessaccess-vbams-access-2010

提问by dlofrodloh

There's a template in Access 2010 called Contacts. On the form there's a comment box, a text box below it and a button. When you enter text into the text box and click the button it adds the text to the comment box and time/date stamps it. If you do it again it preserves the old text in the comment box and adds the new text below it.

Access 2010 中有一个模板,称为联系人。在表单上有一个评论框、一个位于其下方的文本框和一个按钮。当您在文本框中输入文本并单击按钮时,它会将文本添加到评论框中并为其添加时间/日期戳。如果您再次这样做,它会保留评论框中的旧文本并在其下方添加新文本。

I'm pretty new to access, but I would like to be able to add this feature to my database.

我刚开始访问,但我希望能够将此功能添加到我的数据库中。

So I've got a Memo field on the table and form and an input text box and button on the form. Does anyone know what I do from here to get that functionality?

所以我在表格和表单上有一个备注字段,在表单上有一个输入文本框和按钮。有谁知道我从这里做什么来获得该功能?

回答by dlofrodloh

Figured out I had to use the On_Click property of the button and add VBA code to it.

发现我必须使用按钮的 On_Click 属性并向其添加 VBA 代码。

Private Sub cmdAddNote_Click()
 Dim MyDate As String

 MyDate = Now()


 Form_ClientF.txtNotes = vbCrLf + MyDate + vbCrLf + vbCrLf + Form_ClientF.txtAddNote + vbCrLf + vbCrLf + Form_ClientF.txtNotes

 Form_ClientF.txtAddNote = ""

End Sub

回答by Linger

Here is another example of how to do it. I have the following form:

这是如何做到这一点的另一个例子。我有以下表格:

enter image description here

在此处输入图片说明

With the following code:

使用以下代码:

Private Sub cmdAppendComment_Click()
  If (IsNull(txtNewComment.value)) Then
    MsgBox ("Please enter a comment before clicking" & _
            "on the Append Comment button.")
    Exit Sub
  End If

  If (IsNull(txtComment.value)) Then
    txtComment.value = txtNewComment.value & " ~ " & _
               VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
  Else
    txtComment.value = txtComment.value & _
               vbNewLine & vbNewLine & _
               txtNewComment.value & " ~ " & _
               VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
  End If

  txtNewComment.value = ""
End Sub

What this does is verify that the New Comment has something in it. If so, then it checks the Comment to see if it already contains something. If it does, then it appends the new comment to it, else it just assigns the new comment to it. The date and time is added to the end of each comment.

这样做的目的是验证新评论中是否包含某些内容。如果是,那么它会检查 Comment 以查看它是否已经包含某些内容。如果是,那么它将新评论附加到它,否则它只是将新评论分配给它。日期和时间添加到每条评论的末尾。