WithEvents OnAfterInsert not working (1 Viewer)

GK in the UK

Registered User.
Local time
Today, 18:50
Joined
Dec 20, 2017
Messages
274
Have I missed something glaringly obvious ? My WithEvents routines aren't working.


I want to act on AfterInsert of a child form.
I can get a response on Dirty, but when I code for AfterInsert I get Error 2465 Application or Object Error


Here's my code:


Code:
Private WithEvents PurchDaybookDocRef As Access.TextBox
Private WithEvents PurchDaybookAccRef As Access.TextBox
Private WithEvents ChildForm As Access.Form


' ================================================================================
' Remote Events Follow:
' ================================================================================


Private Sub PurchDaybookDocRef_Click()
'MsgBox ("PurchDaybookDocRef_Click !")
' Call the View button
cmdView_Click
End Sub


Private Sub PurchDaybookAccRef_Click()
' Open the customer or supplier record
MsgBox ("PurchDaybookAccRef_Click !")
End Sub


''' CAN'T MAKE THIS WORK
'Private Sub ChildForm_AfterInsert()                      ' BROKEN
'' Code to run on the AfterInsert event of frmInvoice
'' We've added a new invoice document
'MsgBox ("About to requery")
'RequeryIndex
'End Sub


'' CAN'T MAKE THIS WORK
'Private Sub ChildForm_BeforeUpdate(Cancel As Integer)     ' BROKEN
'' Code to run on the BeforfeUpdate event of frmInvoice
'' We've added a new invoice document
'MsgBox ("About to requery")
'RequeryIndex
'End Sub
'
Private Sub ChildForm_Dirty(Cancel As Integer)           ' WORKS
' Code to run on the AfterInsert event of frmInvoice
' We've added a new invoice document
MsgBox ("About to requery")
RequeryIndex
End Sub


Code to open the child form and set the event:


Code:
Private Sub cmdView_Click()
' view an existing document in the index
Dim strFormName As String
' txtDocType is hidden text on the form, put there from each tab control
' when it is clicked, it's the name of the current tab sub form
' Form name comes from tblDocTypes via strFormName
' txtTransHeaderID also comes from the currently highlighted document
strFormName = fFormName(Me.txtDocType)      ' eg 'frmInvoice'
' Open the relevant form according to the document type that we clicked on
DoCmd.OpenForm strFormName, _
                WhereCondition:="TransHeaderID = " & Me.txtTransHeaderID, _
                DataMode:=acFormReadOnly, _
                OpenArgs:=Me.txtDocType & "|" & "Editable"
' Set the event handler, we can't do this until the form is loaded
' This code executes as soon as the child form is loaded
Set ChildForm = Forms(strFormName).Form           ' OK
ChildForm.OnDirty = "[Event Procedure]"           ' WORKS
'ChildForm.OnBeforeUpdate = "[Event Procedure]"    ' Error 2465  app or object error
'ChildForm.OnAfterInsert = "[Event Procedure]"     ' Error 2465  app or object error
                
End Sub     ' cmdView_Click
I can uncomment the OnAfterInset code, comment OnDirty and it breaks.


Why is it only working with the Dirty event (obviously only one set of the test code is uncommented) ?
The child form opens but I immediately get the error message.
 

GK in the UK

Registered User.
Local time
Today, 18:50
Joined
Dec 20, 2017
Messages
274
That was a struggle, I spent a lot of time on this.


It was fixed by changing the line
ChildForm.OnAfterInsert = "[Event Procedure]"
To
ChildForm.AfterInsert = "[Event Procedure]"


Yet, ChildForm.OnDirty = "[Event Procedure]" worked (with the ON prefix)
 

Minty

AWF VIP
Local time
Today, 18:50
Joined
Jul 26, 2013
Messages
10,355
I don't think there is an event call OnAfterInsert - wish I had seen it earlier I may have suggested that.

Glad you got it fixed though.
 

Minty

AWF VIP
Local time
Today, 18:50
Joined
Jul 26, 2013
Messages
10,355
I THINK those are Table Macros introduced in AC2010.

Good spot - you may be right, I read it as a form event from the OP's original post, but my interpretation could well have been off.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 14:50
Joined
May 21, 2018
Messages
8,463
Events have names that start with prepositions telling when they happen which are
Before
On
After

So OnAfter would make no sense.
 

Users who are viewing this thread

Top Bottom