Solved

Access Audit - Looking at the wrong form

Posted on 2014-02-25
5
641 Views
Last Modified: 2014-02-25
Hi,

I am using a well know "audit" for monitoring changes to my database.

It works fine until I start using sub forms.

For example, let's say I have a form called frmMain which holds a form called frmSub

Specifically, the audit has the line;

For Each ctl In Screen.ActiveForm.Controls

Unfortunately, my "active" form is frmMain but I want the audit on frmSub.

I am not sure when my active form is frmMain - the changes took place in frmSub.

As I see it I need to change my audit code to look at frmSub instead of frmMain.

How do I do this?

THanks folks!
0
Comment
Question by:Patrick O'Dea
  • 3
  • 2
5 Comments
 

Author Comment

by:Patrick O'Dea
ID: 39885127
Here is the audit code

Sub AuditChanges(IDField As String, UserAction As String, FormToAudit)

    On Error GoTo AuditChanges_Err
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim ctl As Control
    Dim datTimeCheck As Date
    Dim strUserID As String
    Set cnn = CurrentProject.Connection
    Set rst = New ADODB.Recordset
    rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
    datTimeCheck = Now()
    strUserID = Environ("USERNAME")
    Select Case UserAction
        Case "EDIT"
            For Each ctl In Screen.ActiveForm.Controls
            
                If ctl.Tag = "Audit" Then
                
                MsgBox ctl
                
                
                    If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                        With rst
                            .AddNew
                            ![DateTime] = datTimeCheck
                            ![UserName] = strUserID
                            ![FormName] = Screen.ActiveForm.Name
                            ![Action] = UserAction
                            ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
                            ![FieldName] = ctl.ControlSource
                            ![OldValue] = ctl.OldValue
                            ![NewValue] = ctl.Value
                            .Update
                        End With
                    End If
                End If
            Next ctl
        Case Else
            With rst
                .AddNew
                ![DateTime] = datTimeCheck
                ![UserName] = strUserID
                ![FormName] = Screen.ActiveForm.Name
                ![Action] = UserAction
                ![RecordID] = Screen.ActiveForm.Controls(IDField).Value
                .Update
            End With
    End Select
AuditChanges_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
AuditChanges_Err:
    MsgBox Err.Description, vbCritical, "ERROR!"
    Resume AuditChanges_Exit
End Sub

Open in new window

0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 39885473
Subforms aren't part of the Forms collection in Access (they're actually part of the Controls collection of the parent form), so you need to alter that procedure to pass in a Form object:
Sub AuditChanges(IDField As String, UserAction As String, FormToAudit As Form)

    On Error GoTo AuditChanges_Err
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim ctl As Control
    Dim datTimeCheck As Date
    Dim strUserID As String
    Set cnn = CurrentProject.Connection
    Set rst = New ADODB.Recordset
    rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
    datTimeCheck = Now()
    strUserID = Environ("USERNAME")
    Select Case UserAction
        Case "EDIT"
            For Each ctl In FormToAudit
            
                If ctl.Tag = "Audit" Then
                
                MsgBox ctl
                
                
                    If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                        With rst
                            .AddNew
                            ![DateTime] = datTimeCheck
                            ![UserName] = strUserID
                            ![FormName] = FormToAudit.Name 'Screen.ActiveForm.Name
                            ![Action] = UserAction
                            ![RecordID] = FormToAudit.Controls(IDField).Value ' Screen.ActiveForm.Controls(IDField).Value
                            ![FieldName] = ctl.ControlSource
                            ![OldValue] = ctl.OldValue
                            ![NewValue] = ctl.Value
                            .Update
                        End With
                    End If
                End If
            Next ctl
        Case Else
            With rst
                .AddNew
                ![DateTime] = datTimeCheck
                ![UserName] = strUserID
                ![FormName] = FormToAudit.Name 'Screen.ActiveForm.Name
                ![Action] = UserAction
                ![RecordID] = FormToAudit.Controls(IDFIeld).Value 'Screen.ActiveForm.Controls(IDField).Value
                .Update
            End With
    End Select
AuditChanges_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
AuditChanges_Err:
    MsgBox Err.Description, vbCritical, "ERROR!"
    Resume AuditChanges_Exit
End Sub

Open in new window


Then call it like this for a Form, assuming you're calling this directly from code in the Form's code module:

AuditChanges "YourIDField", "YourUserAction", Me

For a subform, and assuming you're calling this from the Parent form:

AuditChanges "YourIDField", "YourUserAction", Me.NameOfYourSubformControl.Form

If you're calling this somewhere OTHER than the Form's code module:

AuditChanges "YourIDField", "YourUserAction", Forms("YourFormName")

For Subforms:

AuditChanges "YourIDField", "YourUserAction", Forms("YourFormName").NameOfYourSubformControl.Form

If you're dealing with Sub-Subform, you'd have to go furtner:

AuditChanges "YourIDField", "YourUserAction", Forms("YourFormName").NameOfYourSubformControl.Form.NameOfYourSubSubFormControl.Form

and so on for each "level" of your subforms ...

Note that "NameOfYourSubformControl" is the name of the Subform CONTROL on the parent form. This may or may not be the same as the Form being used as a Subform.
0
 

Author Comment

by:Patrick O'Dea
ID: 39886386
Thanks Scott for a superb answer.

Can I just double check one thing.

I will always be calling the Audit from a SubForm.

Can you confirm which is the snippet of code that I should be using to call the Audit.

Thanks.
0
 
LVL 84
ID: 39886476
This one:

AuditChanges "YourIDField", "YourUserAction", Forms("YourFormName").NameOfYourSubformControl.Form

But if you are ALWAYS calling from a subform, you can do this instead:

AuditChanges "YourIDField", "YourUserAction", Me.Parent.NameOfYourSubformControl.Form
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39886484
Thanks Scott.

Got it working now.  Great stuff!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question