-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodChangeLog.bas
More file actions
144 lines (115 loc) · 4.78 KB
/
Copy pathmodChangeLog.bas
File metadata and controls
144 lines (115 loc) · 4.78 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Attribute VB_Name = "modChangeLog"
Option Compare Database
Option Explicit
Sub UpdateChangelog(Frm As Form, RecordID As Variant, Optional strMode As String)
'Track changes to data.
'
'To include a form's control in the changelog, set its Tag property to 'Changelog'.
'Do this for all controls in the form that are to have changes tracked.
'
'
'Call this sub from a form's AfterUpdate event. Example:
'
' Private Sub Form_BeforeUpdate(Cancel As Integer)
' Call UpdateChangelog(Me, Me.Company)
' End Sub
'
'Recordid = the table's primary key, or another field that
'will ensure the user can identify which record was changed.
'
'To include a note in the Notes field of the changelog, include
'VBA code in the controls BeforeUpdate event
'to add a comma and the note to the tag. Example :
'
' Private Sub AssistantsName_BeforeUpdate(Cancel As Integer)
' Dim StrName As String
' StrName = InputBox("Who requested the change?")
' Ctrl.Tag = Ctrl.Tag & ",Change requested by " & StrName
' End Sub
'
'The presence of the comma will prompt the UpdateChangelog sub to
'to treat the tag as an array, with element 1 as the note.
'Structure of the required table tblChangeLog:
'|Name |Type|
'|LogID |AutoNumber|
'|DateTime |Date/Time|
'|UserID |Long Integer|
'|FormName |Text|
'|ControlName |Text|
'|OldValue |Text|
'|NewValue |Text|
'|RecordID |Text|
'|Notes |Text|
Dim Ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
Dim UserID As Long
UserID = DLookup("user_id", "user")
On Error GoTo errHandler
For Each Ctl In Frm.Controls
With Ctl
'Only form controls tagged "Changelog" will be tracked in the changelog.
If .Tag Like "Changelog*" Then
'There is a note attached, so split the tag into an array using the comma
If InStr(.Tag, ",") > 0 Then
Dim TagContent() As String
Dim Notes As String
TagContent = Split(.Tag, ",")
'TagContent(0) = "Changelog", TagContent(1) is the note.
Notes = TagContent(1)
'No note attached
Else
Notes = ""
End If
strControlName = .Name
'Change nulls to empty strings so the expression "varAfter <> varBefore", below,
'will evaluate to either True or False, and never Null.
varBefore = Nz(.OldValue, "")
varAfter = Nz(.Value, "")
'Convert vars to strings
varBefore = CStr(varBefore)
varAfter = CStr(varAfter)
'Escape any apostrophes present..
varBefore = Replace(varBefore, "'", "''")
varAfter = Replace(varAfter, "'", "''")
'Check for strMode = "Delete". In the case of deletions, varBefore will equal varAfter.
'Setting varAfter to "" will make them unequal, and trigger the changelog update.
If Not (IsMissing(strMode)) Then
If strMode = "Delete" Then
varAfter = ""
Notes = "Deleted"
End If
End If
Notes = Replace(Notes, "'", "''")
'Track only fields that have changed.
If varAfter <> varBefore Then
strSQL = "INSERT INTO tblChangelog (UserID, " & _
"FormName, " & _
"ControlName, " & _
"OldValue, " & _
"NewValue, " & _
"RecordID, " & _
"Notes) " _
& "VALUES(" & UserID & ", '" & _
Frm.Name & " ', '" & _
strControlName & "', '" & _
varBefore & "', '" & _
varAfter & "', '" & _
RecordID & "', '" & _
Notes & "');"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
End If
End With
Next
Set Ctl = Nothing
Exit Sub
errHandler:
MsgBox "Error : " & Err.Number & vbNewLine & _
Err.Description & vbNewLine & _
"Procedure : UpdateChangelog", vbOKOnly, "Error"
End Sub