0

I have created an audit trail vba code for my MS-Access database : I copied the code, and got it working for the "New" case:

But I get Error 483 when I try to do an "Update":

Here's the Code:

Option Compare Database

Public Function AuditLog(RecordID As String, UserAction As String)
On Error GoTo AuditErr

Dim DB As Database
Dim rst As Recordset
Dim ctl As Control
Dim UserLogin As String

Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT * from tbl_AuditLog")

UserLogin = Environ("Username")

Select Case UserAction
    Case "New"
        With rst
            .AddNew
            ![EditDate] = Now()
            ![User] = UserLogin
            !FormName = Screen.ActiveForm.Name
            !Action = UserAction
            !RecordID = Screen.ActiveForm.Controls(RecordID)
            .Update
        End With
        
    Case "Delete"
        With rst
            .AddNew
            ![EditDate] = Now()
            ![User] = UserLogin
            !FormName = Screen.ActiveForm.Name
            !Action = UserAction
            !RecordID = Screen.ActiveForm.Controls(RecordID)
            .Update
        End With
        
    Case "Edit"
        For Each ctl In Screen.ActiveForm.Controls
            If (ctl.controltpe = acTextBox _
                Or ctl.ControlType = acComboBox) Then
                If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
                    With rst
                      .AddNew
                        ![EditDate] = Now()
                        ![User] = UserLogin
                        !FormName = Screen.ActiveForm.Name
                        !Action = UserAction
                        !RecordID = Screen.ActiveForm.Controls(RecordID)
                        !FieldName = ctl.ControlSource
                        !OldValue = ctl.OldValue
                        !NewValue = ctl.Value
                        .Update
                    End With
                End If
            End If
        Next ctl
End Select
rst.Close
DB.Close
Set rst = Nothing
Set DB = Nothing

AuditErr:
    If Err.Number = 0 Then
        Exit Function
    Else
        MsgBox Err.Number & " :: Unable to create audit log ::" & Err.Description
    End If
    Exit Function
    
End Function

It breaks at specifically this line:

If Nz(ctl.Value) <> Nz(ctl.OldValue) Then

Now again I copied this code, and in the source for the code it worked. But I feel, that something must be missing here. I just don't know what. Anyone able to see the error, or point me in a good direction to start?

  • `If (ctl.controltpe = acTextBox Or ctl.ControlType = acComboBox)` - are you sure that ever worked? – GSerg May 28 '21 at 16:22
  • When I step into the control during debugging, it gets past that line. So while I am not 100% certain it works, it worked in the code I copied, though I suppose it could be causing the break. – Christopher J. Joubert May 28 '21 at 16:24
  • 1
    So you are saying your controls have both [`ControlType`](https://learn.microsoft.com/en-us/office/vba/api/access.textbox.controltype) and `controltpe`? Are you sure this is the execution marker that gets past the line? Compile time errors will flag when you first enter into the procedure and highlight the wrong line without executing up to it. Is the `Dim ctl As Control` the actual [Access control](https://learn.microsoft.com/en-us/office/vba/api/access.control)? Where does it take you if press Shift+F2 on the word `Control`? – GSerg May 28 '21 at 16:26
  • OMG, not paying attention FTL... I literally just kept reading that as ControlType and didn't notice it was misspelled. Not enough caffeine I think today. – Christopher J. Joubert May 28 '21 at 16:34
  • Add `Option Explicit` to the module's Declarations section (up there with `Option Compare Database`). Then Access will alert you when it encounters something like `controltpe`. – HansUp May 28 '21 at 16:46
  • 1
    @HansUp Not if it's a property or method reference on an [extensible object](https://stackoverflow.com/q/28218373/11683). Then it's still a runtime error. – GSerg May 28 '21 at 16:48
  • 2
    Thank you, @GSerg. That was an interesting and useful read. And I still think they should include `Option Explicit` so that it will alert them to problems it *can* spot. :-) – HansUp May 28 '21 at 17:02

0 Answers0