By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

save record and audit trail

P: 51
I dont it is possible to put this codes together.. I want to prompt the user to save the record and at the same time have a log of audit trail. the codes work out fine separately.


Code for Audit Trail

Option Compare Database
Const cDQ As String = """"
Sub AuditTrail(frm As Form, recordid As Control)
'Track changes to data.
'recordid identifies the pk field's corresponding
'control in frm, in order to id record.
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
On Error GoTo ErrHandler
'Get changed values.
For Each ctl In frm.Controls
With ctl
'Avoid labels and other controls with Value property.
If .ControlType = acTextBox Then
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
'Build INSERT INTO statement.
strSQL = "INSERT INTO " _
& "Audit (EditDate, User, RecordID, SourceTable, " _
& " SourceField, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & cDQ & ", " _
& cDQ & recordid.Value & cDQ & ", " _
& cDQ & frm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
'View evaluated statement in Immediate window.
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End If
End With
Next
Set ctl = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description & vbNewLine _
& Err.Number, vbOKOnly, "Error"
End Sub

Then Insert this Code to the form to be tracked for changes

Private Sub Form_BeforeUpdate(Cancel As Integer)
Call AuditTrail(Me, GenId)
End Sub



Code for Save Record

Option Compare Database
Option Explicit
Public Function SaveRecord()
On Error GoTo Err_SaveRecord
'Provide the user with the option to save/undo
'changes made to the record in the form
If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
Else
DoCmd.RunCommand acCmdUndo
End If
Exit_SaveRecord:
Exit Function
Err_SaveRecord:
End Function

Any help will be highly appreciated.
Apr 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: 51
...i need help..is this possible?
Apr 23 '07 #2

nico5038
Expert 2.5K+
P: 3,072
Ofcourse this is possible, but I would use a lot less code.

Start with adding a [Save] and [Cancel] button to the form.
Next test when the [Save] is pressed or all fields are filled correctly and or the user has changed something has been made like:
Expand|Select|Wrap|Line Numbers
  1. IF Me.Dirty then
  2.   ' the update code
  3. END IF
  4.  
Now issue an Append query with the unique record identifyer like:
currentdb.execute "INSERT INTO tblTraceLog (...) values (.....)"
and close the form to save the data.
Getting the idea ?

Nic;o)
Apr 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.