Question : Using Audittrail - handling if values are null or empty

i download the example from TechRepublic and it seems to work except when the before value is null then I get an error.
 
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   On Error GoTo Form_BeforeUpdate_Error

    gApNo = Me.ApNo
    gWSNO = Me.WS_No
    Call AuditTrail(Me)
   

   On Error GoTo 0
   Exit Sub

Form_BeforeUpdate_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate of VBA Document Form_FE_WS_Main"
End Sub


Option Compare Database
Option Explicit
Const cDQ As String = """"
Sub AuditTrail(frm As Form)
  'Track changes to data.
  'recordid identifies the pk field's corresponding
  'control in frm, in order to id record.
  Dim ctl As Control
  Dim varBefore As Variant
  Dim varAfter As Variant
  Dim strControlName As String
  Dim strSQL As String
  On Error GoTo ErrHandler
     For Each ctl In frm.Controls
        If ctl.Tag = "Audit" Then
            varBefore = Nz(ctl.oldvalue)
 Debug.Print varBefore
           varAfter = Nz(ctl.Value)
Debug.Print varAfter
            If varAfter <> varBefore Then
                strControlName = Nz(ctl.Name)
Debug.Print ctl.Name
                'Build INSERT INTO statement.
                strSQL = "INSERT INTO " _
                   & "Audit (EditDate, User, ApNo, WSNO, MeasNO, " _
                   & " SourceField, BeforeValue, AfterValue) " _
                   & "VALUES (Now()," _
                   & cDQ & Environ("username") & cDQ & ", " _
                   & cDQ & GetgApNo() & cDQ & ", " _
                   & cDQ & GetgWSNO() & cDQ & ", " _
                   & cDQ & GetgMeasNo() & cDQ & ", " _
                   & cDQ & strControlName & cDQ & ", " _
                   & cDQ & varBefore & cDQ & ", " _
                   & cDQ & varAfter & cDQ & ")"
                DoCmd.RunSQL strSQL
Debug.Print strSQL
            End If
        End If
    Next ctl
    Set ctl = Nothing
  Exit Sub

ErrHandler:
  MsgBox Err.Description & vbNewLine _
   & Err.Number, vbOKOnly, "Error"
End Sub
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   On Error GoTo Form_BeforeUpdate_Error

    gApNo = Me.ApNo
    gWSNO = Me.WS_No
    Call AuditTrail(Me)
   

   On Error GoTo 0
   Exit Sub

Form_BeforeUpdate_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate of VBA Document Form_FE_WS_Main"
End Sub

Option Compare Database
Option Explicit
Const cDQ As String = """"
Sub AuditTrail(frm As Form)
  'Track changes to data.
  'recordid identifies the pk field's corresponding
  'control in frm, in order to id record.
  Dim ctl As Control
  Dim varBefore As Variant
  Dim varAfter As Variant
  Dim strControlName As String
  Dim strSQL As String
  On Error GoTo ErrHandler
     For Each ctl In frm.Controls
        If ctl.Tag = "Audit" Then
            varBefore = Nz(ctl.oldvalue)
 Debug.Print varBefore
           varAfter = Nz(ctl.Value)
Debug.Print varAfter
            If varAfter <> varBefore Then
                strControlName = Nz(ctl.Name)
Debug.Print ctl.Name
                'Build INSERT INTO statement.
                strSQL = "INSERT INTO " _
                   & "Audit (EditDate, User, ApNo, WSNO, MeasNO, " _
                   & " SourceField, BeforeValue, AfterValue) " _
                   & "VALUES (Now()," _
                   & cDQ & Environ("username") & cDQ & ", " _
                   & cDQ & GetgApNo() & cDQ & ", " _
                   & cDQ & GetgWSNO() & cDQ & ", " _
                   & cDQ & GetgMeasNo() & cDQ & ", " _
                   & cDQ & strControlName & cDQ & ", " _
                   & cDQ & varBefore & cDQ & ", " _
                   & cDQ & varAfter & cDQ & ")"
                DoCmd.RunSQL strSQL
Debug.Print strSQL
            End If
        End If
    Next ctl
    Set ctl = Nothing
  Exit Sub

ErrHandler:
  MsgBox Err.Description & vbNewLine _
   & Err.Number, vbOKOnly, "Error"
End Sub

Answer : Using Audittrail - handling if values are null or empty

on error goto ctlValueError
 If Nz(ctl.oldvalue, "empty") = True Then
                Exit Sub
            Else
                varBefore = ctl.oldvalue
            End If

ctlvalueError:
end sub
Random Solutions  
 
programming4us programming4us