accessing subform elements

I have a form and a subform within it. I have several controls in the subform. When i try to access the values of the subform from an event triggered by a control in the subform, i get the error message that the form is not present. for example, what should i do for doin the following stuff. I have a button in the subform and on clicking the button, i want to msgbox the current chosen value from a combo box in the subform.
what should the code be and how do i refer to the combo box in the subform? should i refer it based on the mainform?
Who is Participating?
ornicarConnect With a Mentor Commented:
Do like me: Be lazy. This Forms!..blah syntax can be painfull in some cases, but fortunately, there
is the expression builder.

- open your page or form
- create a new macro
- Type SetValue for the instruction in this macro
- at the bottom of the screen, for the first property of this SetValue instruction, click on the button
with 3 points (...) You have the expression builder opened.
- Use to browse through opened forms or pages, until you find your control in one of the windows. Click
- Select and copy/paste the expression generated
- et voilà
the correct syntax is generally


have you tried that syntax?

In a subform you just refer to "me.".
Just make sure your code really is "behind" the subform.
In access 97 you need to double-click the subform to get it's code, in access 2000 you also need to click twice, once to select the subform on the mainform, the second time to "open" the subform's controls.

For the message this should suffide:
msgbox "Combo value: " & me.combo1

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

it also depends on where your button is... if your button is on your main form, you need to refer to the subform first. borrowing from nico's abbreviated syntax, that'd be

me!SubFormName!ControlName (same as using Forms!FormName!SubFormName!ControlName)

if the button is on the subform, you can indeed simply use the Me reference... the difference is that access "knows" that by Me you are referring to the form in question...

btw. based on your question, i'd say that nico has the correct answer... i'm just adding info. ;)

Subforms are actually a two-level hierarchy - they are a control that contains a form.  So, in dot notation:


would return the value in ComboBoxName inside the SubFormControlName subform.  I've used this to do hierarchal locking based on user-ID (with the Enabled, Locked, and UpdatesAllowed properties) from VBA code running under the context of the main form.

Peter Kolbus
I rest "me." case for the form independent coding....
You'll run into this pittfall sooner or later :-)))

Sankar030999Author Commented:
nico, u r rite. but using expression builder was really easy. i didnt use the macro/set value stuff. just wanted to know the expression for accessing the subform and parent form elements from within the subform. the expression builder gave me the correct expr for this.

Thanks for all your inputs.

Hey everyone,
Good stuff but how would you call the click event of a button on a subform from the main form???

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.