Visual Basic Concepts
The following example uses the DataCombo box control to create a data entry screen for the Titles table of the Northwind.mdb sample database. It lets the user enter new products and assign them to existing suppliers by providing a lookup table of all the suppliers' names. When users get to the Supplier field in the entry form, they can choose a supplier from a list box. When they select a supplier, that supplier's SupplierID field is copied into the SupplierID field of the Products table.
To create a lookup table with the DataCombo control
If a Data Source has not been created, follow the steps in "Creating the Northwind OLEDB Data Source."
If the DataGrid, DataCombo, or ADO Data Control is not present in the Toolbox, right-click the Toolbox, and use the Components dialog box to add it.
Property | Setting |
Name | adoDataSource |
ConnectionString | Northwind.udl |
RecordSource | Select * From Products; |
Caption | Products |
Property | Setting |
Name | adoRowSource |
ConnectionString | Northwind.udl |
RecordSource | Select CompanyName, SupplierID From Suppliers; |
Caption | Suppliers |
Visible | False |
Property | Setting |
Name | grdProducts |
DataSource | AdoDataSource |
Caption | Products |
Property | Setting |
Name | dcbSuppliers |
DataSource | adoDataSource |
DataField | SupplierID |
RowSource | adoRowSource |
ListField | CompanyName |
BoundColumn | SupplierID |
Private Sub Form_Load()
' Hide the SupplierID field in the DataGrid control, so the user is
' not confused on which value to change.
grdProducts.Columns("SupplierID").Visible = False
End Sub
You can navigate through the recordset by clicking the arrows on the visible ADO Data control. As you do so, the DataCombo control will update and display the name of the supplier for each product. To edit the SupplierID field, click the DataCombo control's arrow to display a drop-down list, then click again on a different supplier to change the value written to the SupplierID field.