user2836518
user2836518

Reputation:

Best method to allow an event to work on a form

in VBA I have a subform which I use on many forms; however I have a bit of code where I only want this code to work on 'one form'; so if I have the subform with a textbox in form 1, form 2 and form 3, I want the after update event to only work for form 2.

What's the best possible way to go about doing this?

Upvotes: 1

Views: 45

Answers (1)

HansUp
HansUp

Reputation: 97131

A subform has a Parent property. So you can check the Name property of the subform's Parent.

Dim strParent As String
strParent = Me.Parent.Name
If strParent = "form 2" Then
    ' do stuff for form 2
End If

Notes:

  1. That code is intended for the after update event of a text box on the subform. If the target text box is present on the parent form instead, it's simpler; just check the parent form's name directly (Me.Name).
  2. If the subform is opened on its own, ie not as a subform to another form, Me.Parent will throw an error. You would then need to trap that error and ignore it.

Upvotes: 1

Related Questions