Problem with Audit code for Null/Blank records - SOLVED

StuLux

Well-known Member
Joined
Sep 14, 2005
Messages
682
Office Version
  1. 365
Platform
  1. Windows
I am using the following code (adapted from the Microsoft article on Access Audit trail).

The code writes values to text boxes (txtMeasure, txtOldData, txtNewData) on the form and then the query "qAudit" uses the values from these text boxes to append to an audit table. A record is not created when the existing value is Blank or Null, I have tried many combinations of code to get this to happen but am failing somewhere? My latest attempt is indicated below.

Code:
Function Audit()
On Err GoTo Err_Handler

    Dim myForm As Form, ctl As Control
    Set myForm = Screen.ActiveForm
    
'Check each data entry control for change and record old value of Control
    For Each ctl In myForm.Controls
'Only check data entry type controls.
    Select Case ctl.ControlType
        Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox
'Skip fields used to collate changes
        If ctl.Name <> "txtMeasure" Or ctl.Name <> "txtOldData" Or _
        ctl.Name <> "txtNewData" Then
        
        myForm!txtMeasure = ctl.Name 'Applys the name of the ctl to txtMeasure

'If control had previous value, record previous value
            If ctl.Value <> ctl.OldValue Then
                myForm!txtOldData = ctl.OldValue

If IsNull(ctl.OldValue) Or ctl.OldValue = "" Then myForm!txtOldData = "Blank" 'This is the line I am trying to get to work

        myForm!txtNewData = ctl.Value 'Applys new value to txtNewData

'Runs the qAudit append query to create a new record in tblAUDIT
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "qAudit"
        DoCmd.SetWarnings True
            End If
        End If
        End Select
    Next ctl

trynextctl:
    Exit Function

Err_Handler:
    If Err.Number <> 64535 Then
        MsgBox "Error #: " & Err.Number & vbCrLf & "Description: " & Err.Description
    End If
    Resume trynextctl:
End Function
 
Awesome - I wish I could claim it was my diabolical plan to make you work a bit. Really, I slacked off and failed to give you a completely well thought out solution.

I'm really glad you were able to figure out the logic you needed to make it work. It actually is much better when you churn through it and do it yourself - much better understanding of what it's actually doing and it's a lot like the old "give a man a fix" vs "teach a man to fish" analogy.

Mike
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Bumped because I have a question on this.

I took StuLux's modification and have it working great, however, I noticed that it does not capture changes made to controls on subforms, only those changes made to a main form.

I tried the following changes to see if it would remedy the problem:

Code:
Set myForm = Screen.ActiveForm.OnGotFocus

and

Code:
Set myForm = Screen.ActiveForm.SetFocus

However, neither of these worked. Anyone have an idea of how to modify the function to accomplish this?

Thanks in advance for any advice.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top