?
Solved

Access Audit - Looking at the wrong form

Posted on 2014-02-25
5
Medium Priority
?
670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 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 85
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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