Introduction.
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 come from Table, Query, or from Value List. The User is expected to click on the Drop Down control of the Combo Box to display the items and select the required one to insert into the data field. The User can type Values directly into the Source Control of the Combo Box too.
But, the first Property setting out of the following two Property settings of Combo Box allows the user to select items from the existing list only and prevents from typing invalid values directly into the Target Field:
- Limit to List = Yes
- On Not in List = [Event Procedure]
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 in the Source Table of the Combo Box Control first before they can be used for the Combo Box.
For example, assume that you have a Table with a list of Fruits (only two items on 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.
The On-Not-in-List Event.
When the On Not in List Property is set to an Event Procedure; it is executed when the user enters a new value (Orange) manually into the Control-Source 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 manually. 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.
Combo Box Row Source Table.
Create a new Table with a single Field Name: Fruit and select the Data Type Text.
Save the Table Structure and name it 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.
Combo Box Property Settings.
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 Fruit list 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 new 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.
Testing Settings.
Open the Sales Form in the normal view.
Since we have added the Combo Box on the Table Structure it already appears on the form.
Press Ctrl++ (or click on the 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 to the Fruit list Table.
Open the Fruit list 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 to 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 for requery 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.
Add New Item through VBA
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 Event Procedure 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.
Trial Run Program.
Open it in a 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 Fruit List 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 to the list and accepted in the Description Field as well.