[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 684
  • Last Modified:

Access Audit - Looking at the wrong form

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
Patrick O'Dea
Asked:
Patrick O'Dea
  • 3
  • 2
1 Solution
 
Patrick O'DeaAuthor Commented:
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
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
 
Patrick O'DeaAuthor Commented:
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
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
 
Patrick O'DeaAuthor Commented:
Thanks Scott.

Got it working now.  Great stuff!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now