Hi all,
I have built an audit solution to log changes made to data in forms. This all works fine, however I have a problem that people want access to data in a table format, which is based on a Query. Is the any way to amend the below code / make new code to incorporate a way to audit changes made to data via a query?
The track changes runs on all fields that want to be audited and change reason runs after update of form to log the reason for the amendment.
Many thanks
Option Compare Database
Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String
' Returns the login name for Adminstrator use
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
Function TrackChanges()
Dim db As Database
Dim rs As Recordset
Dim strsql As String
Dim strctl As String
strctl = Screen.ActiveControl.Name
strsql = "SELECT Audit.* FROM Audit;"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
If rs.RecordCount > 0 Then rs.MoveLast
With rs
.AddNew
rs!FormName = Screen.ActiveForm.Name
rs!ControlName = strctl
rs!DateChanged = Date
rs!TimeChanged = Time()
rs!PriorInfo = Screen.ActiveControl.OldValue
rs!NewInfo = Screen.ActiveControl.Value
rs!CurrentUser = fOSUserName
rs!recordid = Screen.ActiveForm.[Employee ID]
.Update
End With
Set db = Nothing
Set rs = Nothing
End Function
Function ChangeReason()
Dim db As Database
Dim rs As Recordset
Dim strsql As String
Dim strReason As String
strReason = InputBox("Reason For Changes")
strsql = "SELECT Audit.* FROM Audit;"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
If rs.RecordCount > 0 Then rs.MoveLast
With rs
.AddNew
rs!recordid = Screen.ActiveForm.[Employee ID]
rs!DateChanged = Date
rs!TimeChanged = Time()
rs!CurrentUser = fOSUserName
rs!reason = strReason
.Update
End With
Set db = Nothing
Set rs = Nothing
End Function
I have built an audit solution to log changes made to data in forms. This all works fine, however I have a problem that people want access to data in a table format, which is based on a Query. Is the any way to amend the below code / make new code to incorporate a way to audit changes made to data via a query?
The track changes runs on all fields that want to be audited and change reason runs after update of form to log the reason for the amendment.
Many thanks
Option Compare Database
Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String
' Returns the login name for Adminstrator use
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
Function TrackChanges()
Dim db As Database
Dim rs As Recordset
Dim strsql As String
Dim strctl As String
strctl = Screen.ActiveControl.Name
strsql = "SELECT Audit.* FROM Audit;"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
If rs.RecordCount > 0 Then rs.MoveLast
With rs
.AddNew
rs!FormName = Screen.ActiveForm.Name
rs!ControlName = strctl
rs!DateChanged = Date
rs!TimeChanged = Time()
rs!PriorInfo = Screen.ActiveControl.OldValue
rs!NewInfo = Screen.ActiveControl.Value
rs!CurrentUser = fOSUserName
rs!recordid = Screen.ActiveForm.[Employee ID]
.Update
End With
Set db = Nothing
Set rs = Nothing
End Function
Function ChangeReason()
Dim db As Database
Dim rs As Recordset
Dim strsql As String
Dim strReason As String
strReason = InputBox("Reason For Changes")
strsql = "SELECT Audit.* FROM Audit;"
Set db = CurrentDb()
Set rs = db.OpenRecordset(strsql, dbOpenDynaset)
If rs.RecordCount > 0 Then rs.MoveLast
With rs
.AddNew
rs!recordid = Screen.ActiveForm.[Employee ID]
rs!DateChanged = Date
rs!TimeChanged = Time()
rs!CurrentUser = fOSUserName
rs!reason = strReason
.Update
End With
Set db = Nothing
Set rs = Nothing
End Function