Visual Basic Concepts

Creating a Simple DataCombo Application

See Also

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

  1. Create an OLEDB Data Source for the Northwind database.

    If a Data Source has not been created, follow the steps in "Creating the Northwind OLEDB Data Source."

  2. Create a new Standard EXE project in Visual Basic.

    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.

  3. Add a DataCombo, two ADO Data controls, and a DataGrid control to your form.

  4. In the Properties window, set the properties of the first data control (Adodc1) as shown in the table below.
    Property Setting
    Name adoDataSource
    ConnectionString Northwind.udl
    RecordSource Select * From Products;
    Caption Products

  5. In the Properties window, set the properties of the second data control (Adodc2) as shown in the table below.
    Property Setting
    Name adoRowSource
    ConnectionString Northwind.udl
    RecordSource Select CompanyName, SupplierID From Suppliers;
    Caption Suppliers
    Visible False

  6. In the Properties window, set the properties of the DataGrid control as shown in the table below.
    Property Setting
    Name grdProducts
    DataSource AdoDataSource
    Caption Products

  7. In the Properties window, set the properties of the DataCombo control as shown in the table below.
    Property Setting
    Name dcbSuppliers
    DataSource adoDataSource
    DataField SupplierID
    RowSource adoRowSource
    ListField CompanyName
    BoundColumn SupplierID

  8. Finally, add the following code to the form's code module:
    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
    
  9. Run the project.

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.