Question

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?

Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top