Solved

Access Audit - Looking at the wrong form

Posted on 2014-02-25
5
629 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now