Share via


Visual Basic Concepts

Format Objects

Format objects allow you to format and unformat data passed between a database and a bound object. You can also use the traditional Format function to format your data, but format objects offer these advantages:

  • The Format and Unformat events allow closer control over data formatting.

  • You can set formats either through code or using the IDE; in either case when you use format objects you'll write less code.

  • Format objects give you more formatting types.

Types of formatting available from format objects include the standard types supported by the Format function (currency, date/time, string) plus the types in the following table.

Formatting option Description
Boolean Specifies values displayed in the bound control when data read from the database is a Boolean true or false. Writes a Boolean true or false to the database when the data matches a specified value.
Binary Allows any binary data to be read from and written to the database.
Object Allows an object to be read from and written to the database.
Picture Allows a picture to be read from and written to the database.
Checkbox Values read from the database will determine the Value property for a checkbox. Checkbox Value property settings will determine true/false values written to the database.

Stepping Through the Process

You can set formatting options in code or using property pages available from the bound control's DataFormat property in the property window. When the bound control fetches a record from the database, formatting is applied according to properties of the StdDataFormat object, then the Format event is fired and data is displayed by the bound control.

When data is written back to the database, the formatting you applied goes with it. In most cases this works fine, but if you use the Format property to apply complex formatting you may create strings your database cannot unformat. In these cases you need to unformat the data in the Unformat event before it is written to the database.

Understanding the Object Hierarchy

Format objects include:

  • StdDataFormat object

    This object provides the Format and Unformat events, and it lets you select format types and apply format strings.

  • StdDataFormats collection

    Complex bound controls will usually need more than one StdDataFormat object. In this case the StdDataFormats collection gives you top-level access to the collection.

  • DataValue object

    You can use the DataValue object in the Format and Unformat events to customize formatting beyond what is provided by the StdDataFormat object.

  • DataFormat property

    The DataFormat property of the bound control sets or returns the StdDataFormat object.