Object Model > Working with Objects and Collections |
This section describes how to work with objects and collections in Visual Basic code, with an emphasis on efficiency. Although the concepts are illustrated with True DBList objects and collections, you can apply the same fundamentals to all Visual Basic objects and collections.
A TDBList object is created when you place a True DBList control on a Visual Basic form. TDBList objects created in Visual Basic will have default names of TDBList1, TDBList2, and so forth. You can change the control name in the Visual Basic Properties window at design time. You can also change the control's properties using the property pages at design time and Visual Basic code at run time.
A TDBList object has the following collections: Splits, Columns, SelBookmarks, Styles, Layouts, and PrintInfos. By default, the Splits collection contains one Split object, and the Columns collection contains two Column objects. The Styles collection contains eight default Style objects: Normal, Heading, Footing, Selected, Caption, HighlightRow, EvenRow, and OddRow. The PrintInfos collection contains one PrintInfo object for accessing the system default printer. The SelBookmarks and Layouts collections are initially empty.
You can reference an object in a collection using its zero-based index. For example, the default Split object in a list has an index value of 0. You can read or set the Split object's properties as follows:
Example Title |
Copy Code
|
---|---|
' Read a Split object property variable = TDBList1.Splits(0).Property ' Set a Split object property TDBList1.Splits(0).Property = variable |
You can create a reference to an object in a collection using the collection's Item method. The following code creates a reference to a list's default Split object:
Example Title |
Copy Code
|
---|---|
' Declare Split0 as a Split object Dim Split0 As TrueDBList80.Split ' Set Split0 to reference the first Split in the collection Set Split0 = TDBList1.Splits.Item(0) |
Note the use of the type library qualifier TrueDBList80 in the preceding example. Using the type library qualifier is recommended in order to resolve potential naming conflicts with other controls. For example, if you use another control in the same project that also defines an object named Split, the TrueDBList80 type library qualifier is required, as is the type library qualifier for the other control.
If you are using the OLE DB version of True DBList, the type library qualifier is TrueOleDBList instead of TrueDBList80. The object names are the same. |
Since the Item method is implicit for collections, you can omit it:
Example Title |
Copy Code
|
---|---|
' Declare Split0 as a Split object Dim Split0 As TrueDBList80.Split ' Set Split0 to reference the first Split in the collection Set Split0 = TDBList1.Splits(0) |
You can now use Split0 to read or set the Split object's properties or to execute its methods:
Example Title |
Copy Code
|
---|---|
variable = Split0.Property ' Read a Split object property Split0.Property = variable ' Set a Split object property Split0.Method arg1, arg2, ... ' Execute a Split object method |
Very often, you need to read and set more than one of an object's properties. For example:
Example Title |
Copy Code
|
---|---|
' Read a Split object's properties variable1 = TDBList1.Splits(0).Property1 variable2 = TDBList1.Splits(0).Property2 ' Set a Split object's properties TDBList1.Splits(0).Property1 = variable1 TDBList1.Splits(0).Property2 = variable2 |
This code is very inefficient because each time the object TDBList1.Splits(0) is accessed, Visual Basic creates a reference to the object and then discards it after the statement is completed. It is more efficient to create a single reference to the object up front and use it repeatedly:
Example Title |
Copy Code
|
---|---|
' Declare Split0 as a Split Dim Split0 As TrueDBList80.Split ' Set Split0 to reference the first Split in the collection Set Split0 = TDBList1.Splits(0) ' Read a Split object's properties variable1 = Split0.Property1 variable2 = Split0.Property2 ' Set a Split object's properties Split0.Property1 = variable1 Split0.Property2 = variable2 |
This code is much more efficient and also easier to read. If your Visual Basic application accesses collection objects frequently, you can improve the performance of your code significantly by adhering to these guidelines.
Similarly, you can apply this technique to other objects and collections of True DBList, and of Visual Basic in general. Of particular importance to the list are the Column object and Columns collection:
Example Title |
Copy Code
|
---|---|
' Declare Cols as a Columns collection object, then set it to ' reference TDBList1's Columns collection object. Dim Cols As TrueDBList80.Columns Set Cols = TDBList1.Columns ' Declare Col0 as a Column object, then set it to reference the ' first Column object in the collection. Dim Col0 As Column Set Col0 = Cols(0) ' Read and set the Column object's Property1 variable1 = Col0.Property1 Col0.Property1 = variable1 ' Execute the Column object's Method1 (declared as a Sub) Col0.Method1 arg1, arg2, ... ' Execute the Column object's Method2 (declared as a Function) variable2 = Col0.Method2(arg1) |
Visual Basic also provides an efficient With...End With statement for setting multiple properties of an object without explicitly assigning it to a variable. For example, the following code sets multiple properties of the first column of a list (recall that collections are zero-based):
Example Title |
Copy Code
|
---|---|
With TDBList1.Columns(0) .Property1 = variable1 .Property2 = variable2 End With |
Some collections allow you to reference their members by name. For example, you can reference a Column object using either its index, the name of the database field the column is associated with, or the column's heading caption. Thus, the following statements are equivalent:
Example Title |
Copy Code
|
---|---|
' Declare Col0 as a Column object Dim Col0 As TrueDBList80.Column ' Reference by numeric index Set Col0 = TDBList1.Columns.Item(0) ' Reference by numeric index (Item method is implicit) Set Col0 = TDBList1.Columns(0) ' Reference by database field name Set Col0 = TDBList1.Columns("LAST") ' Reference by column header text (Caption property) Set Col0 = TDBList1.Columns("Last Name") A True DBList Style object can also be referenced by name: ' Declare S as a Style object Dim S As TrueDBList80.Style ' Set S to the list's built-in Normal style Set S = TDBList1.Styles("Normal") ' Set S to the programmer-defined style MyStyle Set S = TDBList1.Styles("MyStyle") |
This code adds a Split object with index 0 to the Splits collection of TDBList1. The original Split object now has an index of 1. Alternatively, you can create a Split object with index 1:
Example Title |
Copy Code
|
---|---|
' Create a Split object with index 1
Dim S As TrueDBList80.Split
Set S = TDBList1.Splits.Add(1)
|
Note that the Add method of the Splits collection is used like a function, with its arguments (here, the split index) enclosed in parentheses. Also, since the Add method always returns a reference to the Split object that was just created, you must precede the assignment statement with the Visual Basic Set keyword.
However, not all collections define their Add method to return a value. If a collection does nothing more than maintain a list of the arguments passed to its Add method, then there is no need for it to return the same item that was just added. In True DBList, the Layouts, SelBookmarks, and ValueItems collections are designed this way. For example, you can use the following code to select the current record in a TDBList control:
Example Title |
Copy Code
|
---|---|
TDBList1.SelBookmarks.Add TDBList1.Bookmark |
Since the SelBookmarks collection manages a list of variants corresponding to selected list rows, its Add method does not return a value, and no assignment statement is needed. This example could also be coded as:
Example Title |
Copy Code
|
---|---|
With TDBList1 .SelBookmarks.Add .Bookmark End With |
Regardless of how a collection implements the Add method, the syntax for removing items is the same. To remove an existing item from a collection, use the Remove method:
Example Title |
Copy Code
|
---|---|
' Remove the Split object with index 1
TDBList1.Splits.Remove 1
|
After this statement is executed, all splits with collection indexes greater than 1 will be shifted down by 1 to fill the place of the removed split. Note that the Remove method is called like a subroutine—its argument is not enclosed in parentheses.
You can determine the number of objects in a collection using the collection's Count property:
Example Title |
Copy Code
|
---|---|
' Set a variable equal to the number of Splits in TDBList1
variable = TDBList1.Splits.Count
|
You can also iterate through all objects in a collection using the Count property as in the following example, which prints the Caption string of each Column object in a list:
Example Title |
Copy Code
|
---|---|
For n = 0 To TDBList1.Columns.Count - 1 Debug.Print TDBList1.Columns(n).Caption Next n |
The Count property is also useful for appending and removing columns:
Example Title |
Copy Code
|
---|---|
' Determine how many columns there are Dim NumCols As Integer NumCols = TDBList1.Columns.Count ' Append a column to the end of the Columns collection Dim C As TrueDBList80.Column Set C = TDBList1.Columns.Add(NumCols) ' Make the new column visible, since columns created ' at run time are invisible by default TDBList1.Columns(NumCols).Visible = True ' The following loop removes all columns from the list While TDBList1.Columns.Count TDBList1.Columns.Remove 0 Wend |
Visual Basic also provides an efficient For Each...Next statement that you can use to iterate through the objects in a collection without using the Count property:
Example Title |
Copy Code
|
---|---|
Dim C As TrueDBList80.Column For Each C In TDBList1.Columns Debug.Print C.Caption Next S |
In fact, using the For Each...Next statement is the preferred way to iterate through the objects in a collection.