Option Compare Database
Option Explicit
Public gstrReason As String
Public Function Delete_Rec(MyForm As Form, UniqID_Field As String, UniqID As String)
On Error GoTo Err_Delete_Rec
MyForm.AllowEdits = True
MyForm.AllowDeletions = True
If MsgBox("Are you sure you want to delete this record", vbYesNo, "Delete this record?") = vbYes Then
Dim ctl As Control
Dim sUser As String
Dim delvals As String
Dim strSQL As String
Const cQUOTE = """" 'That is 2 quotes in sequence
Dim action As String
action = "*** Record Deleted ***"
sUser = Environ("UserName") 'get the users login name
'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 Like "*" & "txt" & "*" Then GoTo TryNextControl 'Skip AuditTrail field.
If Len(ctl.Value) > 0 Then delvals = delvals & "| " & ctl.Name & " = " & ctl.Value & " "
End Select
TryNextControl:
Next ctl
'Broken down into 4 separate variables for ease of view and troubleshooting
strSQL = "INSERT INTO tblAudit ( [User], [DateTime], UniqID_Field, UniqID, Form, [Action], Reason, DelValues)"
strSQL = strSQL & " SELECT " & cQUOTE & sUser & cQUOTE & ", " & cQUOTE & Now & cQUOTE & " , "
strSQL = strSQL & cQUOTE & UniqID_Field & cQUOTE & ", " & cQUOTE & UniqID & cQUOTE & ", "
strSQL = strSQL & cQUOTE & MyForm.Name & cQUOTE & ", " & cQUOTE & action & cQUOTE & ", " & "[Reason for change] AS Expr1" & ", "
strSQL = strSQL & cQUOTE & delvals & cQUOTE & ";"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
DoCmd.SetWarnings False
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
DoCmd.SetWarnings True
End If
MyForm.AllowEdits = False
MyForm.AllowDeletions = False
Exit_Delete_Rec:
Exit Function
Err_Delete_Rec:
Beep
MsgBox Err.Number & " - " & Err.Description
'Resume Exit_Delete_Rec
Exit Function
End Function