简体   繁体   中英

Triggering an event in subform control in Access using VBA

I wanted to trigger an after update event in the subform using vba from the parent form.

In the subform I have:

Private Sub USER_AfterUpdate()
  'After update code
End sub

The subforms name in my parent form is subForm2

So from my main form I am doing:

Call subForm2.Form.USER_AfterUpdate

However, I get

Application-defined or object defined error

I wanted to target the last user field in my subform but I do not mind running an after update event on all of the user field in the sub form.

Either make the function Public :

Public Sub USER_AfterUpdate()
  'After update code
End Sub

or create a separate function to call:

Private Sub USER_AfterUpdate()
    UserAfterUpdate
End sub

Public Sub UserAfterUpdate()
  'After update code
End sub

and then call this (UserAfterUpdate) from the main form.

You may have to use the extended syntax:

Call procedures in a subform or subreport

You may want to use strong typing to reach to your subforms methods. I find this approach much more readable than the extended syntax proposed by Gustav.

Say your subform is named "Form_Subform", and included in a subform control called "subForm2". You could write:

Dim subform as Form_Subform
Set subform = Me.subForm2.Form
Call subform.USER_AfterUpdate

You will find that, after assigning to a strongly typed variable, intellisense will show you all the public subs and functions of your subform (which is not the case with subform.Form , as my best guess there is that it's seen as the parent class Form and not your Form_Subform implementation).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM