In this step, you will add code in the form Create_Instance event to populate the combo box.
1. Your code will add entries from the table xDepartments to ComboBox1. Add code to perform the following:
Clear list ComboBox1
Select fields xDepartmentCode and xDepartmentDescription from table xDepartments
Add entry to list ComboBox1
Note: SELECT / ENDSELECT is a database I/O command that reads records based on entry sequence, by full key, by partial key or generically. See the Technical Guide for further details and examples. Remember you can press F1 on any command in the editor to jump straight to its online documentation.
Your code should look like the following:
Set Com(#com_owner) Caption(*component_desc)
Select Fields(#xDepartmentCode #xDepartmentDescription) From_File(xDepartments)
Note: the combo box definition ComboBox1 can be used in the SELECT command's Fields() parameter, to retrieve the fields defined as columns in the combo box.
The CLR_LIST statement is not strictly needed, since this logic will be executed once only, each time the form is executed. It is good coding practice to clear a list before adding entries.
2. Compile and test your form.
Note: Initially the first entry in the combo box has focus and the fields on the form are populated. When a row in a list component has focus or is selected, the form fields for the list columns are populated.
3. Click on the drop down button to see the first 8 entries. The combo box's DropDownCount property controls how many entries are shown. A Scroll bar enables other entries to be selected.
The list is displayed in the sequence that entries were loaded. In this case they were loaded in department code sequence (xDepartmentCode). You will look later at how entries in a list can be sorted. One solution could be to add an index to the table xDepartments in xDepartmentDescription sequence and use this index to load the combo box.
4. Select a different department. Once again note that the form fields are populated from the selected list row.
Note: The fields on the form are populated when a new entry in the combo box is selected. All list controls return the field values for the selected row to the form variables, automatically. We do not need code to retrieve these values.