Hi.
If you don't already have a form that allows data entry for this table,
then
create one. The following example will use the frmContacts form to add a
record to the tblContacts table, which the form is bound to via a query.
In
this form's OnLoad( ) event, paste the following code:
' * * * * Start Code * * * *
Private Sub Form_Load()
On Error GoTo ErrHandler
If ((Me.OpenArgs = "New") And Not IsNull(Me!txtID)) Then
DoCmd.GoToRecord , , acNewRec
End If
Exit Sub
ErrHandler:
MsgBox "Error in Form_Load( ) in " & Me.Name & " form." & _
vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
End Sub
' * * * * End Code * * * *
txtID is the text box bound to the primary key field, so you may need to
change it to suit your form. Next, for the form with the combo box, in
the
combo box's OnNotInList( ) event, paste the following code:
' * * * * Start Code * * * *
Private Sub cboContact_NotInList(NewData As String, Response As Integer)
On Error GoTo ErrHandler
MsgBox "Please double-click this field" & vbCrLf & _
"to add a new entry to the list.", _
vbInformation + vbOKOnly, "No Matching Record"
Response = acDataErrContinue
Exit Sub
ErrHandler:
MsgBox "Error in cboContact_NotInList( ) in " & Me.Name & " form." &
_
vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
End Sub
' * * * * End Code * * * *
cboContact is the name of the combo box containing the list of items
stored
in the table that you want to add a new record to. Next, in the combo
box's
OnDblClick( ) event, paste the following code:
' * * * * Start Code * * * *
Private Sub cboContact_DblClick(Cancel As Integer)
On Error GoTo ErrHandler
Dim nContactID As Long
If (IsNull(Me!cboContact)) Then
Me!cboContact.Text = ""
Else
nContactID = Me!cboContact
Me!cboContact = Null
End If
DoCmd.OpenForm "frmContacts", , , , , acDialog, "New"
Me!cboContact.Requery ' Get all records.
If (nContactID <> 0) Then
Me!cboContact = nContactID ' Reset to original row.
End If
Exit Sub
ErrHandler:
MsgBox "Error in cboContact_DblClick( ) in " & Me.Name & " form." & _
vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
End Sub
' * * * * End Code * * * *
Save both forms and compile the code. Open the form with the combo box
in
Form View and double-click on the combo box to see the other form open,
awaiting a new entry to be added to the original table. Once the new
record
has been typed, close that form to return to the form with the combo box.
The combo box will now have the newest record in the list, ready to be
selected by the user.
HTH.
Gunny
See
http://www.QBuilt.com for all your database needs.
See
http://www.Access.QBuilt.com for Microsoft Access tips.
(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail
harvesters
for spammers are (e-mail address removed) and (e-mail address removed)
- - -
When you see correct answers to your question posted in Microsoft's
Online
Community, please sign in to the Community and mark these posts as
"Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that questions
answered the quickest are often from those who have a history of
rewarding
the contributors who have taken the time to answer questions correctly.