First time posting, so please excuse if I pull a moron in my questions.
I have the following:
2 audit modules: basEmplAudit and basEmplDeptAudit
1 query (tblEmployee left join tblEmployeeDepartment)
1 form using the above query as record source
For this particular form I'm trying to avoid using a subform for the Department. However, I still want the form to write changes to 2 separate audit tables (tblEmployeeAudit and tblEmployeeDepartmentAudit). I have tags identifying which controls are aligned to these two audit tables.
If I call either the EmplAudit or EmplDeptAudit individually from the form, this code works smoothly every time. However, when I try to combine calling the audits by evaluating the tags I get the "operation is not supported for this type of object" error.
I would appreciate any help you can provide for that form code - - I'm certain that's where the error is and I've tried dozens of permutations to get this working - - I'm also pretty sure this is a newbie mistake...
Here are the basEmplAudit and basEmplDeptAudit modules:
Here is the form code with which I'm having the issue:
I have the following:
2 audit modules: basEmplAudit and basEmplDeptAudit
1 query (tblEmployee left join tblEmployeeDepartment)
1 form using the above query as record source
For this particular form I'm trying to avoid using a subform for the Department. However, I still want the form to write changes to 2 separate audit tables (tblEmployeeAudit and tblEmployeeDepartmentAudit). I have tags identifying which controls are aligned to these two audit tables.
If I call either the EmplAudit or EmplDeptAudit individually from the form, this code works smoothly every time. However, when I try to combine calling the audits by evaluating the tags I get the "operation is not supported for this type of object" error.
I would appreciate any help you can provide for that form code - - I'm certain that's where the error is and I've tried dozens of permutations to get this working - - I'm also pretty sure this is a newbie mistake...
Here are the basEmplAudit and basEmplDeptAudit modules:
Code:
Option Compare Database
Option Explicit
Public Function WriteAuditEmpl(frm As Form, lngID As Long) As Boolean
On Error GoTo err_WriteAuditEmpl
Dim ctlC As Control
Dim strSQL As String
Dim bOK As Boolean
bOK = False
DoCmd.SetWarnings False
' For each control.
For Each ctlC In frm.Controls
If TypeOf ctlC Is TextBox Or TypeOf ctlC Is ComboBox Then
If ctlC.Value <> ctlC.OldValue Or IsNull(ctlC.OldValue) Then
If Not IsNull(ctlC.Value) Then
strSQL = "INSERT INTO tblEmployeeAudit ( tblEmplAuditID, FieldChanged, FieldChangedFrom, FieldChangedTo, User, DateOfChange ) " & _
" SELECT " & lngID & " , " & _
"'" & ctlC.Name & "', " & _
"'" & ctlC.OldValue & "', " & _
"'" & ctlC.Value & "', " & _
"'" & GetUserName & "', " & _
"'" & Now & "'"
'Debug.Print strSQL
DoCmd.RunSQL strSQL
End If
End If
End If
Next ctlC
WriteAuditEmpl = bOK
exit_WriteAuditEmpl:
DoCmd.SetWarnings True
Exit Function
err_WriteAuditEmpl:
MsgBox Err.Description
Resume exit_WriteAuditEmpl
End Function
Code:
Option Compare Database
Option Explicit
Public Function WriteAuditEmplDept(frm As Form, lngID As Long) As Boolean
On Error GoTo err_WriteAuditEmplDept
Dim ctlC As Control
Dim strSQL As String
Dim bOK As Boolean
bOK = False
DoCmd.SetWarnings False
' For each control.
For Each ctlC In frm.Controls
If InStr(ctlC.Tag, "tagEmplDeptAudit") Then
If TypeOf ctlC Is TextBox Or TypeOf ctlC Is ComboBox Then
If ctlC.Value <> ctlC.OldValue Or IsNull(ctlC.OldValue) Then
If Not IsNull(ctlC.Value) Then
strSQL = "INSERT INTO tblEmployeeDepartmentAudit ( tblEmplDeptAuditID, FieldChanged, FieldChangedFrom, FieldChangedTo, User, DateOfChange ) " & _
" SELECT " & lngID & " , " & _
"'" & ctlC.Name & "', " & _
"'" & ctlC.OldValue & "', " & _
"'" & ctlC.Value & "', " & _
"'" & GetUserName & "', " & _
"'" & Now & "'"
'Debug.Print strSQL
DoCmd.RunSQL strSQL
End If
End If
End If
End If
Next ctlC
WriteAuditEmplDept = bOK
exit_WriteAuditEmplDept:
DoCmd.SetWarnings True
Exit Function
err_WriteAuditEmplDept:
MsgBox Err.Description
Resume exit_WriteAuditEmplDept
End Function
Here is the form code with which I'm having the issue:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
Dim x As Integer
Dim y As Integer
Dim ctl As Control
strMsg = "Do you wish to save your changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard All Changes."
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
If iResponse = vbNo Then
DoCmd.RunCommand acCmdUndo
Cancel = True
End If
If iResponse = vbYes Then
For Each ctl In Me.Controls
If InStr(ctl.Tag, "tagEmplAudit") Then
x = WriteAuditEmpl(Me, Me!tblEmplID)
Else
If InStr(ctl.Tag, "tagEmplDeptAudit") Then
y = WriteAuditEmplDept(Me, Me!tblEmplDeptID)
End If
End If
Next ctl
End If
End Sub