The Basics > Configuring Columns at Run Time > Referencing Column Objects |
When a column is added to or removed from a list, the associated Column object is added to or removed from the list's Columns collection. This may cause a change in the index numbers of the existing columns, making it very inconvenient to reference columns numerically. For this reason, True DBList also allows you to reference columns using either the DataField or Caption strings. Thus, the following references are identical:
Example Title |
Copy Code
|
---|---|
TDBList1.Columns(n) ' Reference by the Column index TDBList1.Columns("LAST") ' Reference by the DataField name TDBList1.Columns("Last Name") ' Reference by the Caption string |
Referencing column objects by DataField or Caption is not case-sensitive. TDBList1.Columns("LAST") refers to the same column as TDBList1.Columns("last").
When you reference a Column object and its properties at run time, Visual Basic creates an instance of the object. For example, if you duplicate certain properties of a column:
Example Title |
Copy Code
|
---|---|
TDBList1.Columns("First").Width = _ TDBList1.Columns("Last").Width TDBList1.Columns("First").Alignment = _ TDBList1.Columns("Last").Alignment TDBList1.Columns("First").AllowSizing = _ TDBList1.Columns("Last").AllowSizing |
The Columns("First") and Columns("Last") objects will each be created and discarded three times in the preceding example. The same results are achieved more efficiently by creating object variables that refer to these columns:
Example Title |
Copy Code
|
---|---|
' Declare Column objects Dim FirstCol As TrueDBList80.Column Dim LastCol As TrueDBList80.Column ' Reference First and Last Column objects Set FirstCol = TDBList1.Columns("First") Set LastCol = TDBList1.Columns("Last") ' Copy properties from Last to First FirstCol.Width = LastCol.Width FirstCol.Alignment = LastCol.Alignment FirstCol.AllowSizing = LastCol.AllowSizing |
The same technique can be applied to other objects in Visual Basic. For more details, see Object Model.