Call module (2 Viewers)

Gareth Newman

New member
Local time
Today, 13:01
Joined
Jul 20, 2023
Messages
3
Hi,
I am using a module called "AuditTrail" for recording changes made to records.
It works fine on primary forms with the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
Call AuditTrail(Me, Job_number)

End Sub

with job_number being the primary key index in the linked table.

The form has a number of subforms however that relate to different tables and I cannot get the module to work from these.
I am sure it is just a simple syntax issue but having tried a number of options, I cannot find the answer. Any help would be appreciated.

Gareth
 

Josef P.

Well-known member
Local time
Today, 14:01
Joined
Feb 2, 2023
Messages
826
In the form used as a subform, you can also respond to BeforeUpdate.
The call must then probably be adjusted - Job_number is probably the primary key, which will be different in the subform.

BTW:
I am using a module called "AuditTrail" for recording changes made to records.
Call invokes a procedure in a module.
 

Gareth Newman

New member
Local time
Today, 13:01
Joined
Jul 20, 2023
Messages
3
In the form used as a subform, you can also respond to BeforeUpdate.
The call must then probably be adjusted - Job_number is probably the primary key, which will be different in the subform.

BTW:

Call invokes a procedure in a module.
Tried that with the same code bar the primary key which is "index" in the subform.

>Private Sub Form_BeforeUpdate(Cancel As Integer)
>Call AuditTrail(Me, Index)
>End Sub

When this line executes, I get a run time error 13 "Type mismatch". While the line is highlighted yellow however, if i hover over index, it has the right index number.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:01
Joined
Sep 21, 2011
Messages
14,305
Type mismatch means what is says.
You are supplying the incorrect data type for the function.
What is the data type of Job number and Index?

I would have thought Index is a reserved name and should be avoided?
And it is :(
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:01
Joined
May 21, 2018
Messages
8,529
If your audit trail function argument is declared as integer and index is long you will get an error eventhough both are numeric.
 

Gareth Newman

New member
Local time
Today, 13:01
Joined
Jul 20, 2023
Messages
3
What is the code for the AuditTrail?
The AuditTrail code is:

Code:
Option Compare Database

Const cDQ As String = """"

Public Sub AuditTrail(frm As Form, RecordId As Control)
' Track changes to data
' 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
' Get changed values
For Each ctl In frm.Controls
With ctl
' Avoid labels and other controls with Value property



If .ControlType = acComboBox Or .ControlType = acTextBox Then
If .Value <> .OldValue Then
varBefore = .OldValue
varAfter = .Value
strControlName = .Name
' BUILD INSERT INTO statement
strSQL = "INSERT INTO " _
& "Audit (EditDate, User, RecordID, SourceTable, " _
& " Sourcefield, BeforeValue, AfterValue) " _
& "VALUES (Now()," _
& cDQ & Environ("username") & cDQ & ", " _
& cDQ & RecordId.Value & cDQ & ", " _
& cDQ & frm.RecordSource & cDQ & ", " _
& cDQ & .Name & cDQ & ", " _
& cDQ & varBefore & cDQ & ", " _
& cDQ & varAfter & cDQ & ")"
' View evaluated statement in Immediate window
Debug.Print strSQL
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
End If
'ElseIf .ControlType = acSubform Then
'AuditTrail.frm(ctl.Name).Form
End If


End With
Next
Set ctl = Nothing
Exit Sub

ErrHandler:
MsgBox Err.Description & vbNewLine _
& Err.Number, vbOKOnly, "Error"
End Sub
 
Last edited by a moderator:

MajP

You've got your good things, and you've got mine.
Local time
Today, 08:01
Joined
May 21, 2018
Messages
8,529
Type
Code:
Call AuditTrail(Me, Me.
and tell me if intellisense comes up after "me.", and see "index" is a choice. My guess is you do not (and should not) have a control called index. If you do have a control called index rename it to something else and use the new name instead.
 

Users who are viewing this thread

Top Bottom