Displaying Data in Access Forms and Reports

Sometimes you don't need to go outside of Access to present your data — if you are designing an Access application, displaying data in forms and printing it in reports may be all you need.

t *r j:r Over the years, Access forms and reports have been significantly upgraded. In Access 2007, one long-requested feature has finally arrived in a workable form (I recall an early and unreliable implementation that made a brief appearance in Access 95). Memo fields can now store and display rich text, using the Text Align property, which takes a value of either Plain Text or Rich Text. When you select Rich Text for this property, you can apply various fonts, colors, and other attributes to selected portions of text in a table field or a control bound to that field.

In earlier versions of Office, if you wanted to generate a letter or other document including a block of text with color, bolding, or other attributes applied to selected words or phrases within the block, you had to create a Word letter and use Word's formatting features. In Access 2007, you can produce Access reports with varied formatting within text blocks, displaying text entered into Access memo fields in a textbox on a form.

The sample database for this section is RichText.accdb.

To create a field that can store data in Rich Text format (behind the scenes, this is done using HTML code, but you don't have to worry about writing the code), start by creating a table field of the Memo data type, and selecting Rich Text as the Text Format value (see Figure 1.1).

FIGURE 1.1

Creating a Memo field to hold Rich Text data.

FIGURE 1.1

Creating a Memo field to hold Rich Text data.

F If you select a block of text in a Rich Text-enabled Memo field, you will see a floating toolbar that lets you apply some formatting, including indenting or outdenting, similar to Word (this works fine; see Figure 1.2). However, if you apply bullets or numbering from this toolbar, you will get the bullets or numbers, but the text that runs over one line won't be indented properly, as shown in Figure 1.3. For this reason, I recommend against using these features, unless all the items on your lists are no more than one line in length.

FIGURE 1.2

Indenting text from the floating toolbar in a Rich Text field.

FIGURE 1.2

Indenting text from the floating toolbar in a Rich Text field.

FIGURE 1.3

Incorrect indentation of an item in a numbered list in a Rich Text field.

i Furnaces or boflers covered with Asbestos insulation material winch are included in the abatement project may tiot be operable aftet the abatement for any number of reasons, and you may need to contact an independent HVAC service company, electrical contractor, or your oil company, to restore service to the unit. It 15 probable that the boiler, furnace, andhotwaterwillbe off during the duration of the project.

Create a form bound to the table with the Memo field; you can now enter data into this textbox control and format it with different fonts, color, bolding, and other attributes, as you would while working in a Word document. When you place the cursor into a control with Rich Text enabled, a formatting group on the Form Ribbon is enabled, with a variety of formatting selections, as shown in Figure 1.4.

Figure 1.5 shows the form with a variety of formatting attributes applied to the text in the textbox.

FIGURE 1.4

Selecting formatting for a portion of text in a Rich Text-enabled textbox on a form.

FIGURE 1.4

FIGURE 1.5

A Rich Text-enabled textbox with a variety of formatting applied to portions of its text.

FIGURE 1.5

Once you have applied formatting to the text on a form, you can create a report based on the table, and the formatting will display on the report as well (see Figure 1.6).

FIGURE 1.6

A report showing Rich Text formatting applied in a textbox on a form.

FIGURE 1.6

A report showing Rich Text formatting applied in a textbox on a form.

Creating Access Form Letters

A more realistic example of Rich Text formatting would be a form letter report, with the body of the letter text coming from a Rich Text-enabled field, and the name and address information from a table of contacts or customers. I created a table called tblLetterText in the sample database, with a Rich Text-enabled Memo field to hold the letter body text, and an ID and a LetterType text field. The LetterBody field holds formatted text, as shown in Figure 1.7 (the Rich Text formatting can be seen directly in the table, though you will find it easier to create and edit the rich text in a textbox control on a form).

You can copy and paste formatted text from a Word document into a Memo field with Rich Text enabled (or a textbox bound to such a field), and the formatting will be preserved. However, bullets and numbered lists won't be aligned correctly, so it is best to turn off those features before copying text to Access.

I also created a one-row table to hold information to use in the database; in this case, it has two Rich Text-enabled Memo fields for the letter header and signature information. The form fdlgSelectLetter (bound to the information table, zstbllnfo) lets you edit the header and signature (Figure 1.8) and select a letter type and a contact.

FIGURE 1.7

A table with formatted letter body text.

FIGURE 1.7

A table with formatted letter body text.

FIGURE 1.8

A dialog form for editing header and signature block information and selecting a letter and contact.

FIGURE 1.8

Clicking the Create Letter button opens a filtered Access report displaying name and address data from the selected Contact record, and the letter body from the selected letter type, as depicted in Figure 1.9.

With Rich Text formatting now supported in Access forms and reports, you may not need to produce a Word letter to get the look you want in printed documents. However, compared with Word, Access reports using Rich Text-enabled Memo fields have one significant limitation. In Word, you can place merge fields or DocProperty fields within a block of text, so that merged data or data stored in document properties will print at a certain point in the text, with the surrounding text wrapping as needed, depending on the length of the text in the fields. This is not possible with a Memo field on an Access report, so if you need to embed merge fields or DocProperty fields within the letter body text, you still need to create Word documents.

Was this article helpful?

0 0

Post a comment