Limit to List Combo Box
Combo Boxes on Tables/Forms are for inserting frequently used common values quickly and easily into Data Fields. The Source Data Values of the Combo Box comes from Table or Query. The User is expected to click on the Drop Down control of the Combo Box to display the list of items and select the required item to insert into the data field. The User can type Values into the Source Control of the Combo Box also.
The following two Property setting of Combo Box allows the User to select items from the available list only and prevents from entering invalid values directly into the Target Field:
- Limit to List = Yes
- On Not in List = [EventProcedure]
When the Limit to List Property Value is set to Yes you must select/type values available in the Combo Box list only and other values keyed in manually are not accepted in the Target Field. You must add new items into the Source Table of the Combo Box Control first before they can be used from the Combo Box.
For example, assume that you have a Table with a list of Fruits (only two items in the list now: Apple and Cherry) and you are using this list in a Combo Box on the Sales Form. When the Limit to List Property Value is set to Yes; you will not be allowed to enter the value Orange into the Target Field of the Combo Box.
When the On Not in List Property is set with an Event Procedure; it is executed when the User enters a new value (Orange) manually into the Target Field of the Combo Box. We can write Code in the Event Procedure to add the entered new value into the Combo Box Source Table directly (after taking confirmation from the User) and update the Combo Box on the Form.
This method can save time otherwise needed for opening and adding new items in the Combo Box source Table directly. Besides that adding new values manually in the Source Table will not automatically refresh the Combo Box contents.
Let us try this out using the above example items as Source Data.
- Create a new Table with a single Field Name: Fruit and select the Data Type Text.
- Save the Table Structure with the name: FruitList.
- Open the Table in Datasheet View and key-in Apple and Cherry as two records.
- Close and Save the Table with the records.
- Create another Table with the following Structure:
Table Structure Field Name Data Type Size ID AutoNumber Description Text 50 Quantity Numeric Long Integer UnitPrice Numeric Double
- Before saving the Structure click on the second Field Data Type (Text) Column to select it.
- Click on the Lookup Tab on the Property Sheet below.
- Click on the Display Control Property and select Combo Box from the drop-down control.
- The Row Source Type Property Value will be Table/Query, if it is not then select it from the drop-down control.
- Click on the drop-down control of the Row Source Property and select the Table FruitList from the displayed list of Tables.
- Change Column Width Property and List Width Property Values to 1".
- Change the Limit to List Property Value to Yes.
- Save the Table Structure with the name Sales.
- Open the Table in Datasheet View and add a record with Apple, 100 and 1.5 in Description, Quantity and UnitPrice Fields respectively.
- Close and save the Table with the record.
- Click on the Sales Table to select it and select Form from Insert Menu.
- Create a Form using the Form Wizard in Column Format and save the Form with the name Sales.
- Open the Sales Form in normal view.
Since, we have added the Combo Box on the Table Structure it is already appeared on the form.
- Press Ctrl++ (or click on New Record control on the Record Navigation control) to add a new blank record on the Form.
- Click on the drop-down control of the Combo Box and you will find the list of fruits: Apple and Cherry in it.
- But, you Key-in Orange into the Description field and press Enter Key.
You will be greeted with the following error message:
If you want to enter the value Orange on the Form, first you must add that item into the FruitList Table.
- Open the FruitList Table and add Orange as a new record and close the Table.
But, this action will not refresh the Combo Box contents automatically to add Orange into the list. You have to close the Sales form and open it again before you are able to select Orange from the list. Or you must add a Command Button on the Form and write Code to Re-Query the Combo Box contents.
What we did manually in response to the above error message can be automated by writing a VBA Routine that can be run through the On Not in List Event Procedure. You don't need to close and open the Form to refresh the Combo Box contents either.
- Open the Sales Form in Design View.
- Click on the Description Field to select the Combo Box control.
- Display the Property Sheet (View – -> Properties).
- Find and Click on the On Not in List Property.
- Select EventProcedure from the drop-down list.
- Click on the build button (. . .) to open the VBA Module.
- Copy and paste the following Code into the Module overwriting the top and bottom Procedure lines already appearing in the Module:
Private Sub Description_NotInList(NewData As String, Response As Integer) Dim strmsg As String, rst As Recordset, db As Database If Response Then strmsg = "Entered Item not in List!" & vbCr & vbCr & "Add to List...?" If MsgBox(strmsg, vbYesNo + vbDefaultButton2 + vbQuestion, "Not in List") = vbYes Then Set db = CurrentDb Set rst = db.OpenRecordset("FruitList", dbOpenDynaset) rst.AddNew rst![Fruit] = NewData rst.Update rst.Close Me.Description.Undo Me.Description.Requery Me![Description] = NewData Me.Refresh End If Response = 0 End If End Sub
- Save and Close the Sales Form.
- Open it in normal view.
- Now, type the name of any fruit that is not in the Combo Box list (say Peach) in the Description Field.
You will be greeted with the following Message Box:
- Click the Command Button with the Label Yes to add the new item keyed in the Description Field into the FruitList Table and refresh the Combo Box List automatically.
- Now, click on the drop-down control of the Combo Box and you can see that the new item is added into the list and accepted in the Description Field as well.