New Record in Combo Box

  • Thread starter Thread starter Alison
  • Start date Start date
A

Alison

I have a combo box on a form that locates a Client. When
the client is not in the list, I would like my user to
add a record in a form. When the user closes the form I
would like to apply their new client to the combo box.

Anybody have some sample code that does this? I always
seem to have trouble getting the combo box to requery.
Any help would be appreciated.

Thanks,
Alison
 
Hi Alison,

Try this to get access to recognize new data entered by
your user into the combo box (the ones not-in-the-list)

Add this code to the Not_in_list event of your Clients
Combo box. Assuming that the Clients Info is in a
separate table. Please make suitable changes to the code.

Private Sub ClientName_NotInList(NewData As String,
Response As Integer)
On Error GoTo Err_Handler

Dim dbs As DAO.Database, rst As DAO.Recordset
Dim strMessage As String

strMessage = "Add " & NewData & " to list of Client Names?"

' if user confirms, return reference to Client Names table
'and
' add row for new drawee
If MsgBox(strMessage, vbOKCancel + vbQuestion, "Add Client
Name ") = vbOK Then
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("ClientNames")
With rst
..AddNew
![ClientName] = NewData
..Update
..Close
End With
Set rst = Nothing
Set dbs = Nothing
Response = acDataErrAdded
Else
Response = acDataErrContinue
Me!DraweeNameID.Undo
End If

Exit_here:
Exit Sub
End Sub

To get the latest info in the combo box you could:

1) base the Record Source of the Client Combobox and the
Client Info form in a query. The query would get updated
any time you add/update the Client Form reflecting the
changes in the Combo box OR
2) Enter the following code in the afterupdate event of
the Client Form
Forms!MainForm.ClientComboBox.Requery

This code produces an error if the Client Form is opened
and records edited/added without the main form being open.

Hope this helps.
Cheers
Anand
 
Thanks, Anand!
I'm going to try it right now!!
Alison
-----Original Message-----
Hi Alison,

Try this to get access to recognize new data entered by
your user into the combo box (the ones not-in-the-list)

Add this code to the Not_in_list event of your Clients
Combo box. Assuming that the Clients Info is in a
separate table. Please make suitable changes to the code.

Private Sub ClientName_NotInList(NewData As String,
Response As Integer)
On Error GoTo Err_Handler

Dim dbs As DAO.Database, rst As DAO.Recordset
Dim strMessage As String

strMessage = "Add " & NewData & " to list of Client Names?"

' if user confirms, return reference to Client Names table
'and
' add row for new drawee
If MsgBox(strMessage, vbOKCancel + vbQuestion, "Add Client
Name ") = vbOK Then
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("ClientNames")
With rst
..AddNew
![ClientName] = NewData
..Update
..Close
End With
Set rst = Nothing
Set dbs = Nothing
Response = acDataErrAdded
Else
Response = acDataErrContinue
Me!DraweeNameID.Undo
End If

Exit_here:
Exit Sub
End Sub

To get the latest info in the combo box you could:

1) base the Record Source of the Client Combobox and the
Client Info form in a query. The query would get updated
any time you add/update the Client Form reflecting the
changes in the Combo box OR
2) Enter the following code in the afterupdate event of
the Client Form
Forms!MainForm.ClientComboBox.Requery

This code produces an error if the Client Form is opened
and records edited/added without the main form being open.

Hope this helps.
Cheers
Anand
-----Original Message-----
I have a combo box on a form that locates a Client. When
the client is not in the list, I would like my user to
add a record in a form. When the user closes the form I
would like to apply their new client to the combo box.

Anybody have some sample code that does this? I always
seem to have trouble getting the combo box to requery.
Any help would be appreciated.

Thanks,
Alison
.
.
 
Back
Top