Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Difference between revisions of "Referring to a control on a subform"

From Wiki

Jump to: navigation, search
 
Line 1: Line 1:
 
'''Finding the right names'''
 
'''Finding the right names'''
  
What makes this very confusing is that you refer to the name of the main form, but after that, you must refer to subform controls and never mention the name of a form again. Forms are objects contained by subform controls, so it does not matter what your form is aclled in the database window, once it is used in a subform control, it just becomes an object, so it is very easy indeed to get the names wrong.
+
What makes this very confusing is that you refer to the name of the main form, but after that, you must refer to subform controls and never mention the name of a form again. Forms are objects contained by subform controls, so it does not matter what your form is called in the database window, once it is used in a subform control, it just becomes an object, so it is very easy indeed to get the names wrong.
  
 
     Forms!MainForm
 
     Forms!MainForm

Latest revision as of 12:20, 8 January 2013

Finding the right names

What makes this very confusing is that you refer to the name of the main form, but after that, you must refer to subform controls and never mention the name of a form again. Forms are objects contained by subform controls, so it does not matter what your form is called in the database window, once it is used in a subform control, it just becomes an object, so it is very easy indeed to get the names wrong.

   Forms!MainForm

Is easy enough, but what comes next? Here are some notes on getting what comes next right.

Using the Immediate Window

To ensure you have the correct objects and names, open the form and click an item in the listbox, choose ctrl+G to open the immediate window (it will be part of a code window and have focus), paste the lines starting with a question mark into the immediate window.

This is the name of the control:

   ?screen.ActiveControl.Name
   

This is the name of the subform control that holds the form that the control is on:

   ?screen.ActiveControl.Parent.Name

This is the name of the form that holds the subform control:

   ?screen.ActiveControl.Parent.Parent.Name

If you have a tab control, it is not relevant.

You now have enough information to refer to the subform and control:

   Forms! + result from screen.ActiveControl.Parent.Parent.Name
          + .Form!
          + result from screen.ActiveControl.Parent.Name
          + !
          + result from screen.ActiveControl.Name

So

   Forms!MainForm!ASubformControl.Form!AListbox

If you have spaces or special characters, you will need square brackets:

   Forms![MainForm]![ASubformControl].Form![AListbox]

You may run into problems referring to nested subforms in code, in which case assign a subform to a form object, for example:

   Set frm = Me.[ASubformControl].Form.[ANestedSubformControl].Form
   frm("NameOfAControl").SetFocus
   frm.Recordset.AddNew

getnames.jpg

Using the Query Design Window

With MS Access 2010 and up, you can also use the query design window to get the names of your controls, but you need to have a reasonable idea of what they are before you start. Open the form so you have a reference, and open a query design window. Add any field to the grid and then go to the criteria line and type

   Form!

You will see a list of forms, choose your form and type exclamation mark (!) to get a list of controls, choose the subform control and type stop followed by Form:

   .Form

To get the form property, exclamation mark again will get you a list of subform controls, and so you build up the reference.

getnames2.png

Refer also to: http://access.mvps.org/access/forms/frm0031.htm

841 Rating: 3.1/5 (34 votes cast)