我正在实现审计日志以记录 简短 对数据库更改的描述。我的审计表由自动编号 PK、empID(number)、Description(memo) 和 auditDate(date/time) 组成。我的 empID 和描述被正确插入而没有抛出错误,但我的日期/时间没有被插入。我认为这可能不像错位引号那么简单。我的VBA代码如下:
在 afterInsert 事件中:
Dim strQuery As String
'Dim js As Integer
Dim currDateTime As Date
currDateTime = Now()
strQuery = "INSERT INTO Audits ([emp Number], Description, dateofAudit) VALUES (" & Me.empID & ", '" & "insertion" & "'," & currDateTime & " )"
CurrentDb.Execute (strQuery)
就像我说的,我可以很好地获得前三个值,但是当我尝试插入日期时间时,我遇到了问题。任何输入表示赞赏。希望这不像错位引号那么简单,因为我在提交这个问题之前尝试了大约 4 种引号位置的变化:)
最佳答案
试试这个方法。
strQuery = "INSERT INTO Audits ([emp Number], [Description], dateofAudit)" & _
vbCrLf & "VALUES (" & Me.empID & ", 'insertion', Now())"
也给自己一个机会检查完成的文本字符串。
Debug.Print strQuery
使用这种方法,您就不需要
currDateTime
多变的。日期/时间值将在数据库引擎评估 Now()
时确定。函数 ... INSERT
的时间语句被执行。如果您想要按照原始方法的时间,请格式化
currDateTime
并添加 #
分隔符。strQuery = "INSERT INTO Audits ([emp Number], [Description], dateofAudit)" & _
vbCrLf & "VALUES (" & Me.empID & ", 'insertion', " & _
Format(currDateTime, "\#yyyy-mm-dd hh:nn:ss\#") & ")"
关于ms-access - 将当前日期时间插入审计表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11084663/