ms-access - 将当前日期时间插入审计表

标签 ms-access vba

我正在实现审计日志以记录 简短 对数据库更改的描述。我的审计表由自动编号 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/

相关文章:

vba - 等待表单在 MS Access VBA 中完成更新

SQL CASE 语句 - 缺少运算符错误

jsp - Tomcat 超时?

vba - EXCEL VBA : Append column from one worksheet to the end of another

vba - Excel 公式 -> VBA : Date of each Friday of the current month

r - RODBC 包的替代方案,用于建立从 R 到 MS Access 的连接

database - 更新命令只更新第一行 vb.net

excel - 嵌套 IFERROR、VLOOKUP、IF VBA 代码 + 向下复制

Excel VBA : Frame doesn't appear in front of ListBox

vba - 我的代码正在运行但未插入任何值