A family of Microsoft relational database management systems designed for ease of use.
As the other respondents have pointed out, this can be done quite simply by means of the control's NotInList event procedure. However, a Cities table will in most databases include at least one non-key column other than the city name, e.g. a state or other regional administrative unit in which the city is located. Simply inserting a row transparently via the NotInList event procedure will usually be insufficient, therefore. The NotInList event procedure can still be used, but, rather than inserting a row transparently into the Cities table it will open a form in dialogue mode, passing the new city name to it via the OpenArgs mechanism. The new name is then assigned as the default value for a new record when the cities form opens in dialogue mode.
You'll find an example as NotInList.zip in my public databases folder at:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
In this little demo file the from Contacts form's city combo box's NotInList event procedure is as follows:
Private Sub City_NotInList(NewData As String, Response As Integer)
Dim ctrl As Control
Dim strMessage As String
Set ctrl = Me.ActiveControl
strMessage = "Add " & NewData & " to list?"
If MsgBox(strMessage, vbYesNo + vbQuestion) = vbYes Then
DoCmd.OpenForm "frmCities", _
DataMode:=acFormAdd, _
Windowmode:=acDialog, _
OpenArgs:=NewData
' ensure frmCities closed
DoCmd.Close acForm, "frmCities"
' ensure city has been added
If Not IsNull(DLookup("CityID", "Cities", "City = """ & _
NewData & """")) Then
Response = acDataErrAdded
Else
strMessage = NewData & " was not added to Cities table."
MsgBox strMessage, vbInformation, "Warning"
Response = acDataErrContinue
ctrl.Undo
End If
Else
Response = acDataErrContinue
ctrl.Undo
End If
End Sub
To assign the value passed to the frmCities form as the DefaultValue property of the City control , the frmCities form's Open evemt procedure is as follows:
Private Sub Form_Open(Cancel As Integer)
If Not IsNull(Me.OpenArgs) Then
Me.City.DefaultValue = """" & Me.OpenArgs & """"
End If
End Sub
However, it must be borne in mind that city and town names can legitimately be duplicated. There are 41 Springfields in the USA for instance, of which 5 are in Wisconsin. Consequently, if a city of a particular name has already been included in the Cities table, it will appear in the combo box's list, so the NotInList event procedure cannot be used to insert another city of the same name. It would be possible to concatenate the city and state names in the list, but even then this would not cater for where there are multiple cities of the same name in a single state or other regional unit. It would be necessary to include another distinguishing value in the concatenated expression, e.g. the county. Parsing the string would be possible, and my demo includes an example of this in the case of personal names in it's opening form. A better approach in my view, though, is to not use the NotInList event procedure at all, but to open the form in dialogue mode via a separate button on the original form, and pass the value inserted into the new row in the dialogue form back to the combo box in the original form, requerying the combo box to include the new item in the list. Such a button is included in my demo's opening form to add a new contact name, personal names also being legitimately duplicable of course. The opening form also shows the use of a concatenated value as the visible column of the combo box, and the use of an additional column in the control's list, in this case a concatenated address, to distinguish between contacts of the same name when selecting an item from the list. The same could be done with other legitimately duplicable values such as city names of course.