Basically, a PickList is an Edit control with a down arrow. When the arrow is clicked a List of options is displayed.
MS Access 97
When a table is defined, it is possible to associate a pick list or a lookup table with individual fields. Then, when one of those fields is placed on a form, a correctly configured Combo Box is automatically configured.
Some Combo Boxes are used for other purposes (and, therefore, not associated with fields). For instance, it is possible to Use a ComboBox to Pick A Record.
Adding a Combo Box via the Wizard
|Data||Control source||blank - the field is "unbound"|
|Data||Row Source Type||Table/Query|
|Data||Row Source||SELECT DISTINCTROW [TableName].[id], [TableName].[Country] FROM [TableName];|
When a combo box contains more than one column, the following will populate 2 fields when the user selects one record.
Private Sub Combo0_Click() ' AfterUpdate may be better than Click Me.Text2 = Me.Combo0.Column(1) Me.Text4 = Me.Combo0.Column(2) End SubIt is possible to change a Combo Box to a List box, and vice versa. (Either right click the control and select Change To or select Format / Change To from the main menu.) Each control provides several ways to read the currect selection.
xx = xyz_UIComboBox.Value 'Returns the data in the bound column i = xyz_UIComboBox.ListIndex xx = xyz_UIComboBox.ItemData(i) 'Returns the data in the bound column xyz_UIComboBox.SetFocus ' SetFocus is required if this code is in a button xx = xyz_UIComboBox.Text ' and you want to read the Text property ' Returns the data displayed on the form ' When using the Column property ' The Columns are numbered from zero ' Be careful, the BoundColumn property numbers the columns from one ' The index is the SECOND parameter, ' When typing in the development environment, ' the popup help has the parameters in the wrong order ' The help file has them in the correct order xx = xyz_UIComboBox.Column(column) xx = xyz_UIComboBox.Column(column, index) ' This works xx = xyz_UIComboBox.Column(index, column) ' This FAILS - order in the popup help
xx = xyz_UIListBox.Value 'Returns the data in the bound column i = xyz_UIListBox.ListIndex xx = xyz_UIListBox.ItemData(i) 'Returns the data in the bound column
You can use the Column property to return data from a specified row and column.
Problems Accessing List Elements
xx = combo0.list(2)However, when I try to run the code with MS Access 97 SR2, I get
Compile error: Method or data member not foundI get the same error with
Display_UICombo.Selected(0) = TrueHowever, this works
' Initialize an unattached combobox to the first element in its list Private Sub Form_Open(Cancel As Integer) Display_UICombo = Display_UICombo.ItemData(0) End SubAnother way to accomplish this is to set the ComboBox's Default property to
=[Display_UICombo].[ItemData](0)(The initial equals sign - = - is optional.)
As far as writing values to a list, I get the values from a lookup table (named with an _LU). Tips for Access Database Users by Allen Browne shows how to Fill a ListBox or Combo using code. Wow, what a mess. For more details, in the MS Access help, find combo boxes, setting values / Create a ListBox ... that gets it values from a function, then click the button in step 1.
Now you see why I use lookup tables.
((Lookup_ProductID__UIPickList.[FieldName]="String actually displayed"))
Sorting by the Contents of a Field
In order to sort the data based on the contents of that field, you have to set
Form.OrderBy = Lookup_LocationTypeID.TypeI discovered this by using the Order buttons on the tool bar. There is no help at all on where Lookup_LocationTypeID.Type came from. I had assumed that LocationTypeID should have worked - wrong again.
On the same form, I have a pick list (comboBox) which is used to limit the displayed records. However, the following code
Form.Filter = "LocationTypeID = '" & _ LocationType_Filter_UIPicklist & "'"produced
Run-time error '3079'.I can't figure out why.
The specified field 'LocationTypeID' could refer to more than one table listed in the FROM clause of your SQL statement.
When I deleted the OrderBy property, the code worked perfectly. Perhaps you can't OrderBy and Filter on the same field.
However, the following works
Form.OrderBy = Locations.LocationTypeIDGo figure :)
In the final code, this seems to work
' In the property box Form.OrderBy = Lookup_LocationTypeID.SortOrder ' On the pick list Change event Form.Filter = "Locations.LocationTypeID = '" & _ LocationType_Filter_UIPicklist & "'" Form.FilterOn = True ' For the LocationTypeID pick list, the Row Source is, ' where SortOrder is not displayed SELECT DISTINCTROW LocationType_LU.LocationTypeID, LocationType_LU.Type, LocationType_LU.SortOrder FROM LocationType_LU ORDER BY LocationType_LU.SortOrder;Apparently, MS Access can not tell which query I am referring to.
Based on the Delphi 5 help, the following are equivalent
ComboBox1.Items := 'Some string'; ComboBox1.Items.Strings := 'Some string';
For database access use
DBComboBox1 Must manually set the values of the list DBLookupComboBox1 Automatically sets the list values ListSource Datasource for the list values, Select from the list KeyField The value of this field is saved in DataSource/DataField ListField This is the value displayed - multiple fields are separated by semicolons DataSource This is the datasource where the result is stored DataField This is the field where the result is storedMultiple fields are tricky - there is only one DropDownWidth parameter (it's in pixels and defines to total list width) - there is no way to set the widths of the individual fields (which appear to be about 140 pixels each). The Delphi 5 help does not contain any examples.
This example generates a "view" (based on MS Access and KADAO tables)
KADaoTable_Users_View.SQL select UserID, Last_Name + ', ' + First_Name as Name from users_tbl order by Last_Name;The "view" allows me to concatenate several fields, with puctuation, and preferred sorting, for use in DBLookupComboBox1 without the problems of wierd column widths and only one field being shown in the edit window. (True Views are created in the database definitions ... however, MS Access does not support these. You can use a MS Access Query as a read only view.)
DBLookupComboBox does not have an event that detects when the KeyValue is changed. Therefore, I use both
There is a problem when a DBLookupListBox is used to select a record - when the program begins, none of the list items is highlighted. The following code sets the highlight to the first item in the list.
procedure TForm1.Site_TabSheetShow(Sender: TObject); begin if Sites_DBLookupListBox.SelectedItem <> '' then begin Sites_DBLookupListBox.KeyValue := Sites_DBLookupListBox.SelectedItem; Sites_DBLookupListBoxClick(self); end; end; procedure TForm1.Sites_DBLookupListBoxClick(Sender: TObject); begin Sites_DBCtrlGrid.DataSource.DataSet.Filter := 'Purpose=''' + Sites_DBLookupListBox.SelectedItem + ''''; Sites_DBCtrlGrid.DataSource.DataSet.Filtered := true; end;Notice that the OnClick event is called when