Solved

accessing subform elements

Posted on 2002-03-14
9
421 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:Sankar030999
9 Comments
 
LVL 8

Expert Comment

by:dovholuk
ID: 6865990
the correct syntax is generally

Forms!FormName!SubFormName!ControlName

have you tried that syntax?

dovholuk
0
 
LVL 54

Expert Comment

by:nico5038
ID: 6866001
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

Nic;o)
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6866015
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...

dovholuk
0
 
LVL 8

Expert Comment

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

dovholuk
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

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

me.SubFormControlName.Form.ComboBoxName.Value

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
0
 
LVL 9

Accepted Solution

by:
ornicar earned 100 total points
ID: 6866311
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
it.
- Select and copy/paste the expression generated
- et voilà
0
 
LVL 54

Expert Comment

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

Nic;o)
0
 

Author Comment

by:Sankar030999
ID: 6884727
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.

Sankar
0
 

Expert Comment

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

Itwg
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now