Custom Project Filling a Combo Box with a Disconnected Recordset

This custom project requires that you complete Hands-On 16-2.

1. Create a form as shown in Figure 16-1. Place a combo box control in the form. Change the Name property of this control to cboCompany. Set the Caption property of the label control to Company:.

B!

rmf-i II Combo : Form

P

Jsl

xj

| ' ' ' ' 1

| * Detail

d

11

Compar

n [-rtcxrd

•1

1

Figure 16-1: This custom form is used to demonstrate how you can fill the combo box control with a disconnected recordset.

Figure 16-1: This custom form is used to demonstrate how you can fill the combo box control with a disconnected recordset.

2. Set the form's Caption property to Disconnected Combo.

3. Save the form as frmFillCombo.

4. In the form's properties sheet, activate the Event tab and click the button next to the On Load event name. In the Choose Builder dialog box, select Code Builder and click OK.

5. Complete the Form_Load procedure as shown below.

Private Sub Form_Load()

Dim rst As ADODB.Recordset Dim strRowSource As String Dim strName As String strName = CurrentProject.Path & "\" & _ "Companies.rst"

Set rst = New ADODB.Recordset With rst

.CursorLocation = adUseClient .Open strName, , , , adCmdFile Do Until .EOF

strRowSource = strRowSource & rst!CompanyName & ";" .MoveNext Loop

With Me.cboCompany

.RowSourceType = "Value List" .RowSource = strRowSource End With

Creating and Manipulating Databases with ADO

.Close End With Set rst = Nothing End Sub

To populate a combo box with values, the code in the Form_Load procedure changes the RowSourceType property of the combo box control to Value List and sets the RowSource property to the string obtained by iterating though the recordset.

Figure 16-2: After opening the form prepared in Custom Project 16-1, the combo box is filled with the names of companies obtained via a persisted recordset.

Persisted recordsets are especially handy when you need to support disconnected users or when you want to take data on the road with you. You can save the required set of records to a disk file, send it to your users in remote locations, or take it with you. While disconnected from the database, you or your users can view or modify the records. The next time you connect to the database you can update the original data with your changes using the BatchUpdate method. Custom Project 16-2 demonstrates this scenario.

0 0

Post a comment