Microsoft KB Archive/290655

= Description of using fields and controls on custom forms in Outlook 2002 =

Article ID: 290655

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q290655







For a Microsoft Outlook 97 version of this article, see 168975.



For a Microsoft Outlook 98 version of this article, see 182362.



For a Microsoft Outlook 2000 version of this article, see 207430.



SUMMARY
This article explains the differences between fields within an Outlook item and controls on an Outlook form. In addition, this article also provides an overview of how to use the Outlook object model to reference fields and controls.



The Difference Between Fields and Controls
Fields (also referred to as properties) actually store data within an Outlook item, such as a contact or a task. The Subject field of a mail message is an example of a field within an item. Fields can be either a pre-defined field that Outlook makes available (such as Subject), or a user-defined field that you can create for your own purposes. For additional information about user-defined fields, click the following article number to view the article in the Microsoft Knowledge Base:

290656 How to work with user-defined fields in solutions in Outlook 2002

Controls are objects, such as a text box, a scroll bar, a list box, or a command button, that let users control, enter, or change data. When designing a custom Outlook form, you place controls on a form to display data or choices, perform an action, or make the form easier to read.

Controls by themselves, provide no storage for the data that is associated with them. In the majority of cases, controls are used to display the contents of a field within the item and therefore should be linked, or bound to a particular field.

Using Fields and Controls
All of the pre-defined form controls (To, From, Cc, and such) are automatically bound to corresponding standard Outlook fields. However, if you create a custom form and add a custom control, you should make sure that a field is designated to store the data that is associated with the control.

For example, suppose you want to add a text box to your mail message form that will allow people to enter their office location. When you design the new form, there are a few ways to accomplish this.

The easiest way is to click the New button on the Field Chooser to create the Office Location field. Then you can simply drag the field from the Field Chooser onto the form. Outlook will automatically create a text box on the form and bind it to the Office Location field.

You can also add a text box control by dragging the control from the Control Toolbox. This creates the text box on the form to provide a place to enter the office location. Since the control itself provides no storage for the item, the text you typed into the office location is lost when you send the item to someone. You must also bind the control to a field to provide storage for the data. This way, when someone fills in the field and sends the item, the data is preserved when the item is received by someone else.

How To Place a Control on a Form

 * 1) Create a new item, such as a new message.
 * 2) On the Tools menu, point to Forms and then click Design This Form, to switch to form design mode.
 * 3) On the Form menu, click Control Toolbox.
 * 4) Drag the control type that you want from the Control Toolbox to the place on the form where you want the control to appear.

How To Create a Field to Provide Storage for the Control

 * 1) If the Field Chooser window is not open, on the form Form menu, click Field Chooser.
 * 2) Click New to open the New Field dialog box.
 * 3) In the Name box, type a name for your new field. In the Type list, click to select the data type. In the Format list, click to select the format for the field.
 * 4) Click OK.

To Bind a Control to a Field

 * 1) Starting in form design mode, use the right mouse button to click the control, and then click Properties on the shortcut menu.
 * 2) In Properties, click the Value tab.
 * 3) Click Choose Field to select the field you want to bind to this control. You can also click New to create a new field instead of using the Field Chooser.

Using VBScript to Change Field and Control Values
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

The Outlook object model syntax for referencing controls is quite different from the syntax for referencing fields. The following examples use Visual Basic Scripting Edition (VBScript) to illustrate syntax, but the same code examples can be used with Outlook Visual Basic for Applications, or Automation if you make appropriate modifications for referencing the Item or Inspector objects.

Syntax for Accessing a Control on a Form
Generic: Item.GetInspector.ModifiedFormPages(&quot;PageName&quot;) _ .Controls(&quot;ControlName&quot;).Property =

Example: Item.GetInspector.ModifiedFormPages(&quot;Message&quot;) _ .Controls(&quot;OfficeLoc&quot;).Visible = True

Syntax for Accessing a Standard Outlook Field
Generic: Item.FieldName = 

Example: Item.Subject = &quot;This is a new subject&quot;

Syntax for Accessing a User-defined Field
Generic: Item.UserProperties.Find(&quot;FieldName&quot;) = Value

Example: Item.UserProperties.Find(&quot;OfficeLoc&quot;) = &quot;Blg 4, 1234&quot;

Usage Examples
In the &quot;OfficeLoc&quot; example above, these two example lines of code have the same effect on the form. Item.UserProperties.Find(&quot;OfficeLoc&quot;) = &quot;Blg 4, 1234&quot;

-or-

Item.GetInspector.ModifiedFormPages(&quot;Message&quot;).Controls _ (&quot;OfficeLoc&quot;).Text = &quot;Blg 4, 1234&quot; The first example changes the Office Location field to a new value and the second line changes the text box control Text property to the new value. Since the control is bound to the field, a change made in either place affects the other.

Tips for When to Use Each Method
  When you want to change a &quot;property&quot; (color, visibility, bold, italic) of a control, you must use the control syntax. You cannot set control properties by changing the field. Example:   Make the Office Location textbox not visible

Correct:   Item.GetInspector.ModifiedFormPages(&quot;Message&quot;) _ .Controls(&quot;OfficeLoc&quot;).Visble = False

Incorrect: Item.UserProperties.Find(&quot;OfficeLoc&quot;).Visible = False   When you want to change the value of the data (text, number, date) use the field syntax. Example:   Change the office location to &quot;Building 5&quot;

Preferred: Item.UserProperties.Find(&quot;OfficeLoc&quot;) = &quot;Building 5&quot;

Also works: Item.GetInspector.ModifiedFormPages(&quot;Message&quot;) _ .Controls(&quot;OfficeLoc&quot;).Text = &quot;Buiding 5&quot;

Example:   Change the subject to &quot;This is a subject&quot;

Preferred: Item.Subject = &quot;This is a subject&quot;

Also works: Item.GetInspector.ModifiedFormPages(&quot;Message&quot;) _ .Controls(&quot;Subject&quot;).Text = &quot;This is a subject&quot;  Programmatically using the Controls collection requires that the form is open. When the form is closed, controls are not available. However, fields are always available and you can use references to the field in your code at all times. For example, suppose you want to be able to start from one item and click a button that changes the subject for a different item. If you use fields, you do not have to write code to open the form. If you access the control, your code must include lines to display the form. Displaying the item takes more code and more time. Fields work whether the form is loaded or not, regardless of what page the control is on, and affect all the controls that display that field, not just one. Using fields also insures that other fields calculated from the original update.</ul>

<div class="references_section">