Tutorials > Tutorial 3 - Lookup Combo Update with Two Data Controls |
This tutorial illustrates the most common use of the True DBCombo control: to provide a list of choices from one database that can be used to update a field in another database. When the user selects an item from the combo's list portion, it becomes available to the updatable data source, and also appears in the text box portion of the combo where it can be edited. Likewise, when the user enters a value in the text box, the list portion of the control attempts to position to a matching item. If a match is found, the BoundText property is set based on the field specified by the BoundColumn property.
Start a new project.
Place two Data controls (Data1, Data2), a True DBCombo control (TDBCombo1), and a Microsoft Data Bound Grid Control 5.0 (DBGrid1) on the form (Form1) as shown in this figure.
Set the DatabaseName (Data control) property of Data1 and Data2 to TDBLDemo.mdb, the RecordSource (Data control) property of Data1 to CustType, and the RecordSource (Data control) property of Data2 to the following SQL statement:
Example Title |
Copy Code
|
---|---|
SELECT FirstName, LastName, CustType FROM Customers |
Set the following properties of TDBCombo1: RowSource = Data1, ListField = TypeDesc, DataSource = Data2, DataField = CustType, MaxComboItems = 5 and BoundColumn = TypeId.
Set the DataSource property of DBGrid1 to Data2.
Run the program and observe the following:
TDBCombo1 retrieves the database schema information from the Data control specified by the RowSource property.The contents of the text box portion of TDBCombo1 (Normal) match the CustType field of the current row from Data2 (2).Note that the field names are used as the default column headings.
Change the current record in DBGrid1 and notice that the combo's text box portion shows the appropriate CustType.For example, if the second row (Gordon) is current, the combo displays Distributor, which corresponds to 4 in the grid's CustType column. If the third row is current, the combo displays Prospective.
Click the combo's drop-down button to show its list portion. Note that when you click on a row in the list, the value specified by the ListField column will be entered into the text box portion of the combo. Also, the combo will update the current record of the data control specified by the DataSource property with the TypeId corresponding to the TypeDesc column. The update does not take place immediately, but occurs when you move the current row in DBGrid1 or Data1.
To end the program, press the End button on the Visual Basic toolbar. You have successfully completed Tutorial 3.