In AuditTrail Convert Control names to Caption (1 Viewer)

Falcon88

Registered User.
Local time
Today, 18:56
Joined
Nov 4, 2014
Messages
299
Hiii all Dears

i have a code that makes user changes in every table .
i uses a special Names for my fields , for that i uses captions always.
the code returns names of fields that changs , i want it to returns the caption of the field .

Is that possible ?

My code like this :
Code:
Public Function Audit_Trail(MyForm As Form)
On Error GoTo Err_Audit_Trail
    
'ACC2000: How to Create an Audit Trail of Record Changes in a Form
'http://support.microsoft.com/default.aspx?scid=kb;en-us;197592
    
'    Dim MyForm As Form
    Dim ctl As Control
    Dim sUser As String
'    Set MyForm = Screen.ActiveForm
'    sUser = "User: " & UsersID 'You need to identify your users if you are not using Access security with workgroups.
    sUser = CurrentUser
    
    'If new record, record it in audit trail and exit function.
    If MyForm.NewRecord = True Then
        MyForm!AuditTrail = MyForm!tbAuditTrail & "New Record added on " & Now & " by " & sUser & ";"
        Exit Function
    End If
    
    'Set date and current user if the form (current record) has been modified.
    MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & vbLf & "Changes made on " & Now & " by " & sUser & ";"
    
    'Check each data entry control for change and record old value of the control.
    For Each ctl In MyForm.Controls
    
    'Only check data entry type controls.
    Select Case ctl.ControlType
    Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox
        If ctl.Name = "tbAuditTrail" Then GoTo TryNextControl 'Skip AuditTrail field.
            'If new and old value do not equal
            If ctl.Value <> ctl.OldValue Then
                MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: " & ctl.Value
            'If old value is Null and new value is not Null
            ElseIf IsNull(ctl.OldValue) And Len(ctl.Value) > 0 Or ctl.OldValue = "" And Len(ctl.Value) > 0 Then
                MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Was Previoulsy Null, New Value: " & ctl.Value
            'If new value is Null and old value is not Null
            ElseIf IsNull(ctl.Value) And Len(ctl.OldValue) > 0 Or ctl.Value = "" And Len(ctl.OldValue) > 0 Then
                MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Name & ": Changed From: " & ctl.OldValue & ", To: Null"
            End If
    End Select
    
TryNextControl:
    Next ctl
    
Exit_Audit_Trail:
    Exit Function
    
Err_Audit_Trail:
    If Err.Number = 64535 Then 'Operation is not supported for this type of object.
        Exit Function
    ElseIf Err.Number = 2475 Then 'You entered an expression that requires a form to be the active window
        Beep
        MsgBox "A form is required to be the active window!", vbCritical, "Invalid Active Window"
    Else
        Beep
        MsgBox Err.Number & " - " & Err.Description
    End If
    Resume Exit_Audit_Trail
    
End Function
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:56
Joined
Jul 9, 2003
Messages
16,271
Try Something like:-

= MyForm!tbAuditTrail & vbCrLf & ctl.Controls(0).Caption & ":
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:56
Joined
Jul 9, 2003
Messages
16,271
BTW, if any of the controls lack a label it will complain.

Sent from my SM-G925F using Tapatalk
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:56
Joined
Jul 9, 2003
Messages
16,271
Try This:-

Code:
 MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Controls(0).Caption & ": Changed From: " & ctl.OldValue & ", To: " & ctl.Value
 

Falcon88

Registered User.
Local time
Today, 18:56
Joined
Nov 4, 2014
Messages
299
Try This:-

Code:
 MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf & ctl.Controls(0).Caption & ": Changed From: " & ctl.OldValue & ", To: " & ctl.Value

Ok

but if any of the controls lack a label ....

I try this :
Code:
 MyForm!AuditTrail = MyForm!tbAuditTrail & vbCrLf &  Nz(ctl.Controls(0).Caption,ctl.name) & ": Changed From: " & ctl.OldValue  & ", To: " & ctl.Value

but stay gives error
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 16:56
Joined
Jul 9, 2003
Messages
16,271
Can you add a label to the lacking controls?
 

Falcon88

Registered User.
Local time
Today, 18:56
Joined
Nov 4, 2014
Messages
299
Can you add a label to the lacking controls?
Very thanks ,

I have a Continuous form , the labe in the Form Header and the Control in the Form Details .how to connect them ? or to makes that code to returns the label that is in the form header ?
 

Users who are viewing this thread

Top Bottom