Solved

Access Audit - Looking at the wrong form

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

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