Microsoft KB Archive/137626

= How to Use DBC Field Captions on Visual FoxPro Forms & Reports =

Article ID: 137626

Article Last Modified on 12/11/1999

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition

-



This article was previously published under Q137626



SUMMARY
The Database Container allows you to enter captions for the fields of a table. However, there is no FieldCaption control that you can place on a form to display a field's caption or to automatically update a field's label on the form should you choose to change the field's caption in the database header. The examples in this article show you how to use DBGETPROP to create a link between field labels on a form and the field captions in the database so that changes propagate.



Example One: Setting the Label for a Standard Text Box Field
 Create a database that contains a table. Add a caption to one or more of the table's fields. Create a new form. Place a label and a text box on it. Add the table to the data environment of the form, and set the ControlSource property of the text box to a field that has a caption.  Add the following code to the Load event of the form: OPEN DATABASE < >   Add the following code to the Init event of the Label object: WITH This .Caption = ; DBGETPROP( .Parent.Text1.ControlSource, ;         "FIELD", "CAPTION" ) ENDWITH  Save and run the form. Note that the caption you assigned to the field is displayed by the label control.</li></ol>

Example Two: Setting Headings of a Grid
Normally, a grid with a ColumnCount of -1 will use the Caption property to detrmine the headings used for columns. However, if you specify the number of columns, the headings are determined by the Caption of the header object, not by the Caption property of the ControlSource field. The following code placed in a grid's Init event will set the grid headings when an instance of the grid is created: LOCAL iLoop, iMax, cSource

SET DATABASE TO < >

WITH This && Determine Number of Columns in Grid iMax = .ColumnCount

&& Get the Table Name used by the Grid && This is necessary because unlike a text box control, && the ControlSource property of a column does not preface && the field name with the table name, so the code must && do this manually. Note that "." is included. cSource = .RecordSource + "."

&& Loop through each column FOR iLoop = 1 TO iMax WITH .Columns(iLoop)

&& Set Header Caption .Header1.Caption = ; DBGETPROP( cSource + .ControlSource, ;              "FIELD", "CAPTION" ) ENDWITH NEXT iLoop ENDWITH

Example Three: Using Captions in Report Headers
To use field captions in reports, create a field (not a text label) whose expression is DBGETPROP("tablename.fieldname","FIELD","CAPTION").

NOTE: Here tablename.fieldname is the actual name of the table and field.

When the report runs, the expression will evaluate to the proper caption.

<div class="references_section">