Audit Trail (1 Viewer)

I had that problem earlier. I found out that when a form with subform is displayed and edits are being done to the subform the active form is the parent form and not the subform.

This seems to be the effect that this line in the Audit Trail had:

Set frm = Screen.ActiveForm

My solution:

1. Pass the form as an argument of the function

Function AuditTrail(frm as Form)

2. Comment out these lines in the code

'Dim frm as Form
'Set frm = Screen.ActiveForm

3. Call the function in the BeforeUpdate event of the form and or subform as
follows:

Call AuditTrail(Me)


HTH,

Lyn

Hy nice with this audit trail , but i still did not manage to solve this problem when shoud record changes from a subform ...
I try this code from your post but still nothing ....

I have one main form with 1 subform and 4 subforms in 1 subform ......
Even so ... changes are only in subform ....still i get the error that can not be found tblauditrail

Please help with an exact sample of beforeupdate....

thanks in advance
 
Hy nice with this audit trail , but i still did not manage to solve this problem when shoud record changes from a subform ...
I try this code from your post but still nothing ....

I have one main form with 1 subform and 4 subforms in 1 subform ......
Even so ... changes are only in subform ....still i get the error that can not be found tblauditrail

Please help with an exact sample of beforeupdate....

thanks in advance
chelbosul:

What code did you try in the before update event?
 
After School Program Needs your help (sdd audit trail)

Hello,

I have simple after school program. I like to add audit trail to expense form. Although, I've been trying to apply the code, I've been unsuccessful. Anyone who's has coding knowledge should be be able to apply it easily using the code given... I've attached my file and it'll be great if someone can add the code for me and maybe I can learn to for the future reference.

Thank you,
 

Attachments

Re: After School Program Needs your help (sdd audit trail)

Hello,

I have simple after school program. I like to add audit trail to expense form. Although, I've been trying to apply the code, I've been unsuccessful. Anyone who's has coding knowledge should be be able to apply it easily using the code given... I've attached my file and it'll be great if someone can add the code for me and maybe I can learn to for the future reference.

Thank you,
Here's your database back with an audit trail implemented. Look at the code behind the form's Before Update event and Delete event. You would just have to do similar on any other forms.
 

Attachments

After School Program needs help with Audit trail code??

Hello,

I am currently building after school program to better track expenses. I wanted to add audit trail to expense budget form and Expense Items form. Someone who has vba code knowledge should be able to easily apply the code. But due to my limited knowledge, I can't understand the code that's posted. It'll be great if someone can help us out. I've attached my database and links to vba code for audit trail.

Again, thank you for your help.
 

Attachments

Oh dear, dare I re-awaken the much disturbed beast?!

If anyone's willing to assist, I've implemented the 'table' version of the audit trail, and works for my main form, but not my subform (which is based on a real frankenstein of tables and queries), although despite the error, it does log the changes in the audit trail table.

I've read the entire thread, but as my VBA abilities range between zero and fleeting, I can't remove the 3251 error (Operation is not supported for this type of object).

The erroring (on subform only) code I'm using is below..

Code:
Option Compare Database
Option Explicit
Dim dbs As DAO.Database
Dim sAuditTable As String
Dim sSQL As String
Dim sTable As String        'Table where the record is being edited
Dim CTL As Control          'The control in the form being edited
Dim sFrom As String         'Original Data in the control
Dim sTo As String           'What the original data was changed to
Dim sPCName As String       'Name of the PC that is being used
Dim sPCUser As String       'Name of the User on the Networked PC
Dim sDBUser As String       'Name of the Database User
Dim sDateTime As String     'Date and Time of the change
'===========================================================================================
'
'   This Audit Trail will track changes to existing records.
'   In the "Before Update" event of the FORM enter the following:
'
'       Call AuditTrail(Me.Form, [RecordID])
'
'   Make sure to create a table called "tbl_AuditLog" and have the following fields:
'   (A table will be created automatically if it does not exist)
'
'       1. RecordID (This is a unique number)
'       2. txt_Table (This is the table where the record was changed)
'       3. lng_TblRecord (This is the RecordID number from the record being changed)
'       4. txt_Form (This is the form being used to edit the record)
'       5. txt_Control (This is the data entry control (field) that was edited
'       6. mem_From (This is the original data in the control (field)
'       7. mem_To (This is what the original data was changed to)
'       8. txt_PCName (This is the name of the PC used to edit the record)
'       9. txt_PCUser (This is the name of the user logged onto the PC)
'       10. txt_DBUser (This is the name of the person looged on to the databse if used)
'       11. dat_DateTime (This is the date and time the record was edited.)
'
'
'   The inspiration behind this code is from:
'       1. support.microsoft.com/default.aspx?scid=kb;en-us;197592
'       2. access-programmers.co.uk/forums/showthread.php?t=44231
'
'
'   Be sure to enable the "Microsoft DAO 3.6 Object Library" Reference
'
'============================================================================================
 
 
Public Function AuditTrail(frm As Form, lngRecord As Long)
On Error GoTo Error_Handler
 
    '----------------------------------------------------------------------
    '   Skips this procedure if a new record is being entered in the form
    '----------------------------------------------------------------------
    'If frm.NewRecord = True Then
    '    Exit Function
    'End If
 
    '----------------------------------------------------------------------
    '   Checks to see if the tbl_AuditLog Exists
    '   Creates the table if it does not exist
    '----------------------------------------------------------------------
    Set dbs = CurrentDb
    dbs.TableDefs.Refresh
 
    sAuditTable = "tbl_AuditLog"
    On Error Resume Next
    If IsNull(dbs.TableDefs(sAuditTable)) Then
        'Table does not exist
        On Error GoTo Error_Handler
        sSQL = "CREATE TABLE tbl_AuditLog([RecordID] COUNTER PRIMARY KEY, [txt_Table] TEXT(50), [lng_TblRecord] LONG, " & _
                "[txt_Form] TEXT(50), [txt_Control] TEXT(50), [mem_From] MEMO, [mem_To] MEMO, [txt_PCName] TEXT(50), " & _
                "[txt_PCUser] Text(50), [txt_DBUser] Text(50), [dat_DateTime] DATETIME);"
        DoCmd.SetWarnings False
        DoCmd.RunSQL sSQL
        DoCmd.SetWarnings True
    Else
        'Table Exists. Do Nothing
        On Error GoTo Error_Handler
 
    End If
    Set dbs = Nothing
 
 
 
    '----------------------------------------------------------------------
    '   Runs through each control on the form and checks for edits/changes
    '----------------------------------------------------------------------
    For Each CTL In frm
 
        Select Case CTL.ControlType     'Only checks data entry type controls.
            Case acTextBox, acComboBox, acListBox, acOptionGroup
 
                sFrom = Nz(CTL.OldValue, "Null")
                sTo = Nz(CTL.Value, "Null")
 
                If sFrom <> sTo Then
 
                    '-----------------------------------
                    '   Gets the required Info
                    '-----------------------------------
                    sTable = frm.RecordSource
                    sPCName = Environ("COMPUTERNAME")
                    sPCUser = Environ("Username")
                    sDBUser = "Me"      'Get Username from the database login
                    sDateTime = Now()
                    sSQL = "INSERT INTO tbl_AuditLog ([txt_Table], [lng_TblRecord], [txt_Form], [txt_Control], " & _
                           "[mem_From], [mem_To], [txt_PCName], [txt_PCUser], [txt_DBUser], [dat_DateTime]) " & _
                           "VALUES ('" & sTable & "', '" & lngRecord & "', '" & frm.Name & "', " & _
                           "'" & CTL.Name & "', '" & sFrom & "', '" & sTo & "', '" & sPCName & "', " & _
                           "'" & sPCUser & "', '" & sDBUser & "', '" & sDateTime & "')"
                    DoCmd.SetWarnings False
                    DoCmd.RunSQL sSQL
                    DoCmd.SetWarnings True
 
                End If
        End Select
    Next CTL
 
Error_Handler_Exit:
   Exit Function
Error_Handler:
    MsgBox ("Error No: " & Err.Number & vbCrLf & vbCrLf & "Error Description: " & Err.Description)
    Err.Clear
    Resume Error_Handler_Exit
End Function

If I add in the following, to the Error Handler code, the error doesn't happen, but the changes aren't rceorded in the audit table either...

Code:
If Err.Number = 3251 Then 'Operation is not supported for this type of object.
  Exit Function
End If

I could really do with some gentle guidance, or a full on verbal whooping (former is preferred).

Thanks lovely people!
 
So sorry to be impatient, but I'd be so very grateful of some help on this.
 
You need to provide the correct function call. For a subform you would need to use

Call AuditTrail(Me.SubformControlNameHere.Form, "Unique Field NameHere in Quotes", Me.SubformControlNameHere.Form![RecordIDFieldNameHere])

where SubformControlNameHere refers to the control on the parent form which HOUSES the subform. You need to use its name instead of the subform name unless the subform control name and the subform name are exactly the same.
 
Last edited:
I was wrong - it would be in the subform's BEFORE UPDATE event and since it would be on that form, you should be able to just use
Code:
Call AuditTrail(Me, "FieldNameHereInQuotes", Me!FieldnameHere)
I was all turned around with my other answer and I realized it as I read it again.
 
You need to provide the correct function call. For a subform you would need to use

Call AuditTrail(Me.SubformControlNameHere.Form, "Unique Field NameHere in Quotes", Me.SubformControlNameHere.Form![RecordIDFieldNameHere])

where SubformControlNameHere refers to the control on the parent form which HOUSES the subform. You need to use its name instead of the subform name unless the subform control name and the subform name are exactly the same.

Thanks Bob,

My lack of intelligence means I might need a little clarification!

The subform, is called 'change_frm_change_to_sam_link_subform', which is housed by a tab control called 'change_tabs', on a tab called 'Linked SAMS'.

The unique record fieldname is called 'change_id'.

I'm a bit confused, as the tab control doesn't seem to have a BeforeUpdate Event, and neither does the tab itself.

The subform does though.

What am I missing?
 
I was wrong - it would be in the subform's BEFORE UPDATE event and since it would be on that form, you should be able to just use
Code:
Call AuditTrail(Me, "FieldNameHereInQuotes", Me!FieldnameHere)
I was all turned around with my other answer and I realized it as I read it again.

Aha, thanks Bob, did't see your correction above! I'll get on it now, thanks so much.
 
Aha, thanks Bob, did't see your correction above! I'll get on it now, thanks so much.

Hmmm, ok, I don't seem to be able to make any record changes, or record additions with the BeforeEvent set, but can when it's removed.

Well, it lets me make a change, but won't let me move away from the record (new or edited), until I press escape to undo :confused:
 
Post the entire code you used in the FORM'S BEFORE UPDATE event.
 
Post the entire code you used in the FORM'S BEFORE UPDATE event.

OK, so with the line:

Code:
Call AuditTrail(Me, "change_id", Me!change_id)

... in the BeforeUpdate event field, I'm getting an error when adding / updating records in the subform...

"Change Tracking Database can't find the object '
Call AuditTrail(Me, "change_id", Me!change_id).' If '
Call AuditTrail(Me, "change_id", Me!change_id)
'is a new macro or macro group, make sure that you have saved it and typed it's named correctly."

Mucho stuck!
 
You put that code in the VBA Window and NOT in the event property?

See here for the correct place to put that code:
http://www.btabdevelopment.com/ts/eventcode

I've tried both the Event Property (which results in the message above)and the VBA Window, under BeforeEvent..

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call AuditTrail(Me, "change_id", Me!change_id)
End Sub

The latter, results in me not being able to create a record, or edit a record in the subform, meaning I have to press escape to revert to how the record was.
 
I've tried both the Event Property (which results in the message above)and the VBA Window, under BeforeEvent..

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Call AuditTrail(Me, "change_id", Me!change_id)
End Sub

The latter, results in me not being able to create a record, or edit a record in the subform, meaning I have to press escape to revert to how the record was.
The second one is correct but if you can't edit, then is change_id really the field on the subform that you should be trying to capture? What is the primary key of the data for the SUBFORM, not the main form. If it is, then is change_id really the way it is spelled or is there a space in the field name?
 
The primary key field on the subform is 'sam_id_full' (which I've tried in the call).

Whether it makes any difference, the subform displays (in datasheet view) all linked projects, to the project shown on it's parent form.

The subform has a record source as follows:

SELECT [change_qry_change_to_sam_link].[linked_sam_id], [change_qry_change_to_sam_link].[sam_id_full], [change_qry_change_to_sam_link].[sam_title], [change_qry_change_to_sam_link].[sam_status], [change_qry_change_to_sam_link].[sam_submitter_name], [change_qry_change_to_sam_link].[sam_installation_date], [change_qry_change_to_sam_link].[sam_expr4], [change_qry_change_to_sam_link].[sam_ccompliance_date], [change_qry_change_to_sam_link].[change_id] FROM [change_qry_change_to_sam_link]

The only editable field is the linked_sam_id field, which, when a SAM ID is linked to the main project is added to a table used solely for the purpose of tracking these links.

Main form and sub form are linked by 'change_id', which I've tried referencing in the AuditTrail call, and I've also tried 'linked_sam_id' and 'sam_id_full.

I've muddled my way through to this point, and the database is working as required, but slotting the audit trail functionality in, is proving difficult (for the subform anyway)!
 
Can you upload a copy of your database? I am having trouble figuring out why it isn't working (I've done it before with subforms and haven't had any of that trouble).
 
Can you upload a copy of your database? I am having trouble figuring out why it isn't working (I've done it before with subforms and haven't had any of that trouble).

Hey Bob, I think doing that would help a lot, but it relies on several other databases, and the data and many fields would need to be cleared down / renamed (due to the place I work), so unfortunately it's not an option for me :(
 

Users who are viewing this thread

Back
Top Bottom