Introduction.
Data records from a table will be added as items in a Collection object, with the Description field values used as the Key parameter for each item.
The source values for a ComboBox on a form are also taken from the Description field of the table. Selecting an item from the ComboBox will use its value as the key to retrieve the corresponding record from the Collection object and display the field values in unbound text boxes on the form.
For experimentation, we have created a small table, Table 1, with a few sample records.
The Table image is given below:
The Table Structure image is given below for reference.
The Sample Demo Form.
We have designed a small form with a Combo box on the header of the Form. The Row Source Property of the Combo box is set with the following SQL:
SELECT [Table1].[Desc] FROM Table1 ORDER BY [Desc];
To pick the Desc field value as the Row Source of the Combo Box.
Four text boxes with their Child Labels are added in the Detail Section of the Form. The Text Box Name Property values are set with the same name as each field on the Table, for easier reference in the Program, in the same order, they appear on the Table.
The design image of the frmtable1 is given below:
The Normal View of the Form frmTable1, with data displayed from the Collection Object, is given below for reference. The Combo box contents are also shown in the Form.
The following code runs in the frmTable1 Form’s Class Module. If you have already designed the above Form, ensure that the text boxes are set with the field name of the Table structure shown above. The Combo box name is cmbDesc. You can download a database with the Code from the link given at the end of this Page.
Form Module Code.
Copy and paste the following Code into the frmTable1’s Class Module:
Option Compare Database Option Explicit Private Coll As Collection Dim txtBox() As String Private Sub Form_Load() Dim db As Database Dim rst As Recordset Dim flds As Long, k As Long
Dim frm As Form, Sec As Section, ctl As Control Dim Rec() As Variant, strKey As String 'Open Table1 to upload records into Collection Object Set db = CurrentDb Set rst = db.OpenRecordset("Table1", dbOpenDynaset) 'get record fields count flds = rst.Fields.Count - 1
'Set Detail Section of Form to scan for Text Boxes Set frm = Me Set Sec = frm.Section(acDetail) 'Redim txtBox() to save Textbox names from Form 'to display field values ReDim txtBox(0 To flds) As String 'Get Text Box Names & save into txtBox() Array from Detail Section of Form 'this will be used in ComboBox AfterUpdate Event Procedure k = 0 For Each ctl In Sec.Controls If TypeName(ctl) = "TextBox" Then txtBox(k) = ctl.Name k = k + 1 End If Next
'instantiate Collection Object Set Coll = New Collection
'Redimension Rec Array for number of fields in Table ReDim Rec(0 To flds) As Variant 'Add each record into the Collection Object Do While Not rst.EOF 'Get current record field values into Rec Variant Array For k = 0 To flds Rec(k) = rst.Fields(k).Value Next
'Description Field Value as Key strKey = rst.Fields("Desc").Value 'Add record to the Collection Object with Key Coll.Add Rec, strKey rst.MoveNext Loop rst.Close Set rst = Nothing Set db = Nothing End Sub
Private Sub cmbDesc_AfterUpdate() Dim strD As String, R As Variant Dim j As Long, L As Long, H As Long 'Get Selected Collection Key from ComboBox strD = Me![cmbDesc] 'Retrieve the record from Collection 'using Collection KEY and save the field 'Values into the Variant Variable R = Coll(strD) L = LBound(R) H = UBound(R) 'Add Field Values into corresponding Text Boxes For j = L To H Me(txtBox(j)) = R(j) Next Me.Refresh End Sub Private Sub Form_Unload(Cancel As Integer) 'Remove Collection from Memory on Form Close Set Coll = Nothing End Sub
This is how it works:
All Records from the Table are added as the Collection Object Items in the Form_Load() Event Procedure. The record description Field (Desc) value is used as the Key parameter value of the Item method.
The Desc field values are also used as Combo Box List values on the Form.
When the user selects an item from the Combo Box, the cmbDesc_AfterUpdate() event procedure retrieves the record from the Collection Object, using the Key value chosen from the combo box, and displays the record field values in the Text Boxes on the Form.
The Objects are cleared from memory when the Form is closed.
In the declaration area of the module, we declare the Collection object Coll and an empty array txtBox().
Within the Form_Load event procedure, the Database object db and Recordset object rst are declared. Next, the variable flds is declared to store the count of record fields.
Form, Section, and Control objects are also declared. These are used to locate text boxes on the form, collect their Name property values, and store them in the txtBox array.
A Variant array Rec() is used to temporarily hold record field values before adding them as a single record item to the Collection object.
The string variable strKey is used to assign the record’s Description field value, which will serve as the key for the current record in the Collection object. Each key in the Collection must be unique.
Note: The VBA code lines are commented appropriately. Go through the code line by line a second time to fully understand its purpose.
The Form_Load() event procedure does the following:
The procedure opens Table1 and reads the field count of the first record, storing it in the variable flds.
The form’s Detail Section is assigned to the sec object variable.
All TextBox controls within the Detail Section of the form are located, and their Name properties are collected into the txtBox() array.
Next, the Collection object is instantiated as the object variable Coll.
At the start of the Do While…Loop, the field values of the current record are added to the Rec Variant array.
The Description (Desc) field value is saved into the string variable strKey.
The statement
Coll.Add Rec, strKey
adds the current record’s values from Rec as a new item in the Collection, using strKey as the key.The statement rst.MoveNext advances the record pointer to the next record, and this process repeats until all records in the table have been added to the Collection.
Finally, the Table1 recordset is closed.
At the Form Load Event Procedure, all the records in the Table are loaded into the Collection Object. The Combo Box in the Form’s Header Section is populated with the values from the table’s Description field.
When a user selects an item from the Combo Box, the cmbDesc_AfterUpdate() event procedure is triggered.
The selected Combo Box value is stored in the variable strD, which is then used in the statement R = Coll(strD) to retrieve the corresponding record array from the Collection using strD as the Key. Alternatively, R = Coll.Item(strD) works equally well.
Notice that the Variant variable R is not explicitly declared as an array. VBA automatically determines the correct data type and dimensions based on the record retrieved from the Collection.
The next steps in the VBA code calculate the lower and upper bounds of the array and use them as control values in a For … Next loop. This loop copies the record field values into the corresponding Text Boxes on the Form, using the Text Box names stored in the txtBox array.
Download the Demo Database.


- MS-Access Class Module and VBA
- MS-Access VBA Class Object Arrays
- MS-Access Base Class and Derived Objects
- VBA Base Class and Derived Objects-2
- Base Class and Derived Object Variants
- Ms-Access Recordset and Class Module
- Access Class Module and Wrapper Classes
- Ms-Access and Collection Object Basics
- Ms-Access Class Module and Collection Object
- Table Records in Collection Object and Form
- Dictionary Object Basics
- Dictionary Object Basics-2
- Sorting Dictionary Object Keys and Items
- Display Records from Dictionary to Form
- Add Class Objects as Dictionary Items
- Update Class Object Dictionary Item on Form