I am amending some Code I found online for an audit table, I need to store additional information in the table that is associated with some forms but not others.
I have researched about putting optional variables in, but I read this only works with the type VARIANT.
Is there a way to make a string optional as my fields contain text?
here is my code so far;
Thanks
Kev
I have researched about putting optional variables in, but I read this only works with the type VARIANT.
Is there a way to make a string optional as my fields contain text?
here is my code so far;
Code:
Sub AuditChanges(IDField As String, UserAction As String, Optional UserID As String, Optional DeviceID As String, Optional SimID As String)
On Error GoTo AuditChanges_Err
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim ctl As Control
Dim datTimeCheck As Date
Dim strUserID As String
Set cnn = CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open "SELECT * FROM tblAuditTrail", cnn, adOpenDynamic, adLockOptimistic
datTimeCheck = Now()
strUserID = Environ("USERNAME")
Select Case UserAction
Case "EDIT"
For Each ctl In Screen.ActiveForm.Controls
If ctl.Tag = "Audit" Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
If IsMissing(Screen.ActiveForm.Controls(UserID).Value) = True Then
![User] = "NA"
Else
![User] = Screen.ActiveForm.Controls(UserID).Value
End If
If IsMissing(Screen.ActiveForm.Controls(SimID).Value) = True Then
![Sim] = "NA"
Else
![Sim] = Screen.ActiveForm.Controls(SimID).Value
End If
If IsMissing(Screen.ActiveForm.Controls(DeviceID).Value) = True Then
![Sim] = "NA"
Else
![Device] = Screen.ActiveForm.Controls(DeviceID).Value
End If
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
.Update
End With
End If
End If
Next ctl
Case "New"
For Each ctl In Screen.ActiveForm.Controls
If ctl.Tag = "Audit" Then
If Nz(ctl.Value) <> Nz(ctl.OldValue) Then
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
If IsMissing(Screen.ActiveForm.Controls(UserID).Value) = True Then
![User] = "NA"
Else
![User] = Screen.ActiveForm.Controls(UserID).Value
End If
If IsMissing(Screen.ActiveForm.Controls(SimID).Value) = True Then
![Sim] = "NA"
Else
![Sim] = Screen.ActiveForm.Controls(SimID).Value
End If
If IsMissing(Screen.ActiveForm.Controls(DeviceID).Value) = True Then
![Sim] = "NA"
Else
![Device] = Screen.ActiveForm.Controls(DeviceID).Value
End If
![FieldName] = ctl.ControlSource
![OldValue] = ctl.OldValue
![NewValue] = ctl.Value
.Update
End With
End If
End If
Next ctl
Case Else
With rst
.AddNew
![DateTime] = datTimeCheck
![UserName] = strUserID
![FormName] = Screen.ActiveForm.Name
![Action] = UserAction
![RecordID] = Screen.ActiveForm.Controls(IDField).Value
If IsMissing(Screen.ActiveForm.Controls(UserID).Value) = True Then
![User] = "NA"
Else
![User] = Screen.ActiveForm.Controls(UserID).Value
End If
If IsMissing(Screen.ActiveForm.Controls(SimID).Value) = True Then
![Sim] = "NA"
Else
![Sim] = Screen.ActiveForm.Controls(SimID).Value
End If
If IsMissing(Screen.ActiveForm.Controls(DeviceID).Value) = True Then
![Sim] = "NA"
Else
![Device] = Screen.ActiveForm.Controls(DeviceID).Value
End If
.Update
End With
End Select
AuditChanges_Exit:
On Error Resume Next
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
AuditChanges_Err:
MsgBox Err.Description, vbCritical, "ERROR!"
Resume AuditChanges_Exit
End Sub
Kev