Naming Controls

3 4

Before you start placing controls on a form, you should consider how they will be named. When you drag fields to a form, the controls are given, by default, the same names as the fields to which they are bound. For example, if you drag the LastName field to a form, the text box control that appears on the form is also called LastName. If you click a tool in the Toolbox to place a control on a form, the control appears with a name such as Frame4 (the control type with a number appended).

Problems exist with both types of default names. If a text box has the same name as its field (LastName), you might get a circular reference error (see the Troubleshooting sidebar "The control displays #Error? or #Name when I expect to see the name of the data source or the result of an expression" for more solutions for this type of error). If a control is named Frame4, you have no clue as to what the name represents-for example, when you select a control from the drop-down control selector list on the Formatting toolbar in Design view. To avoid these problems, you might want to use the Leszynski Naming Convention (LNC) control name tags to identify controls, as listed in Table 6-2. The LNC tag is used as a prefix to a base name, which is the field name for bound fields and a descriptive word or phrase for unbound controls. Finding the option group used to select a shipper is much easier if the control is named fraShippers rather than Frame4!

For more details about using the LNC, see Chapter 20, "Customizing Your Database Using VBA Code."

Table 6-2. LNC control tags

Control Tag

Bound Object Frame

frb

Check Box

chk

Combo Box

cbo

Command Button

cmd

Custom

ocx

Hyperlink

hlk

Image

img

Label

lbl

Line

lin

List Box

lst

Option Button

opt

Option Group

fra

Page (on a tab control)

pge

Page Break

brk

Rectangle (shape)

shp

Subform/Subreport

sub

Text Box

txt

Toggle Button

tgl

Unbound Object Frame

fru

Access Control Name Requirements

Access imposes the following restrictions on control names:

  • Control names can't be longer than 64 characters.
  • Control names can't include a period (.), an exclamation point (!), a grave accent (`), or brackets ([]).
  • Control names can't begin with leading spaces.
  • Control names can't include ASCII characters 0 through 31 (control characters).
  • Control names can't include a double quotation mark (") (for projects).

Apart from these restrictions, you can use any combination of numbers, letters and punctuation marks (including spaces). However, I recommend simplifying control names by following these guidelines:

  • Keep control names to 30 characters or fewer.
  • Use only letters and numbers; avoid using punctuation marks and spaces.

Shorter names allow easier selection of a control in the drop-down control selector list on the Formatting toolbar, which isn't very wide (see Figure 6-6). Avoiding punctuation marks prevents possible problems when you're working with control names in VBA code.

figure 6-6. keeping control names short makes it easier to select the appropriate control in the control selector list.

Figure 6-6. Keeping control names short makes it easier to select the appropriate control in the control selector list.

Troubleshooting - The control displays #Error? or #Name when I expect to see the name of the data source or the result of an expression

There are several possible causes for these errors:

  • Problem: The field name in the Control Source property might be inaccurate, deleted from the table, or renamed.

    Solution: If this is the case, simply reselect a field from the record source, using the field list.

  • Problem: An expression might have a syntactical error, such as a missing equal sign at the beginning or no brackets surrounding field names.

    Solution: Fix any errors in the expression, using the Expression Builder to make sure the syntax is correct.

  • Problem: A circular reference error might have occurred because an expression refers to a field that has the same name as the control. Access itself gives bound controls the same names as their fields. If you use the Form Wizard to create a form, you can see this for yourself. For example, Figure 6-7 shows a control named AuthorID bound to the AuthorID field on a form created by the AutoForm: Tabular Wizard. A circular reference error would result if you had an expression including a reference to the Sales field as the control source of a text box named Sales.

    figure 6-7. this control has the same name as its control source field, which could lead to confusion during development.

    Figure 6-7. This control has the same name as its Control Source field, which could lead to confusion during development.

    Solution: The easiest solution is to rename the control with the appropriate tag (for example, txtSales) so that a reference to the field name can't be confused with the control name.

The quickest way to prevent circular reference errors with control names is to run my LNC Renaming COM add-in (described in Chapter 15, "Using Add-Ins to Expand Access Functionality") to rename all controls on a form according to the LNC. Whether you use the LNC Renaming COM add-in to do the renaming or rename the controls on a form manually, using the control tags listed earlier in Table 6-2 will give you the benefits of a naming convention when working with controls.



Microsoft Access Version 2002 Inside Out
Microsoft Access Version 2002 Inside Out (Inside Out (Microsoft))
ISBN: 0735612838
EAN: 2147483647
Year: 2005
Pages: 172
Authors: Helen Feddema

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net