使用VBA插入到创建更改日志

时间:2014-10-13 19:52:13

标签: vba ms-access access-vba

我正在尝试在任何字段更改时创建更改日志。我希望它记录属性,经理和更改日期。我查看INSERT INTO并按照指示编写了这段代码,但是我收到了错误。

Private Sub Manager_AfterUpdate()

INSERT INTO tbl_ManagerChangeLog (Property, NewMan, [Date Change])
    VALUES (Me.ID, Me.Manager, DATE())

End Sub

我收到错误,"编译错误:预期:声明结束"

感谢任何帮助。

这是最适合我的代码。谢谢你的帮助。     Private Sub Manager_Change()

Dim dbs As Database, PropID As Integer, ManNam As String, ChangeDate As Date

    Set dbs = CurrentDb()
    PropID = Me.ID
    ManNam = Me.Manager
    ChangeDate = Date

    dbs.Execute " INSERT INTO tbl_ManagerChangeLog " & "(Property, NewMan, DateChange)VALUES " & "(" & PropID & ", " & ManNam & ", #" & ChangeDate & "#);"

End Sub

2 个答案:

答案 0 :(得分:1)

您必须使用来自currentDb对象的Execute funcion运行SQL查询,并构建连接值的查询值:

Private Sub Manager_AfterUpdate()
   Dim sSQL as String
   sSQL = "INSERT INTO tbl_ManagerChangeLog (Property, NewMan, [Date Change]) " & _
          "VALUES (" & Me.ID & ",'" & Me.Manager "'," & _ 
                    "#" & format(DATE(),"YYYY-MM-DD HH:NN:SS") & "#)"

   currentDB.Execute sSQL
End Sub

答案 1 :(得分:0)

尝试以下VBA来运行SQL查询。

Private Sub Manager_AfterUpdate()

Dim strSql As String , Db As dao.Database 

strSql = "INSERT INTO tbl_ManagerChangeLog (Property, NewMan, [Date Change]) VALUES (Me.ID, Me.Manager, DATE());" 

'Use Current Database 
Set Db = CurrentDb() 

'Run the SQL Query 
Db.Execute strSql 

End Sub