Combo Box

Fgrill220

New Member
Joined
Dec 9, 2004
Messages
2
Happy Holidays All!

I have a form called Projects with a combo box that has a list of names from another table Project Managers, how can I add a new name to the table without leaving the current form?

Thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
The best way to do that is to use the not_in_list event.

If you right click on the combo, and go to the code builder, you can select (from the top right list of events) the not_in_list event. This will fire whenever the user tries to type something into the combo that's not in the list.

At this point, you can use a little piece of dynamic SQL to add the value to the table to which this combo looks.

The basic idea is this:

Code:
Private Sub cmbContact_NotInList(NewData As String, Response As Integer) 

    Select Case MsgBox("Add new contact?", vbYesNo) 
        Case vbYes 
            insSQL = "INSERT INTO tblContact( contact ) " & _ 
                     "VALUES ( """ & NewData & """ );" 
            DoCmd.SetWarnings False 
            DoCmd.RunSQL (insSQL) 
            DoCmd.SetWarnings True 
            Response = acDataErrAdded 
        Case vbNo 
            Me.Undo 
            Response = acDataErrContinue 
    End Select
End Sub

Your combo will have a different name. Also, in the SQL statement:
INSERT INTO tblContact( contact )
'contact' is the name of the field in your table to which you will be adding the new data you've just tried to type into the combo.

If you can, take the spaces out of your names, that's goint to cause troubles down the road.
 
Upvote 0
I get an compile error - variable not defined and highlights insSQL= here is the code:

Private Sub PManager_ID_NotInList(NewData As String, Response As Integer)

Select Case MsgBox("Add new contact?", vbYesNo)
Case vbYes
insSQL = "INSERT INTO Project Managers( PM Name ) " & _
"VALUES ( """ & NewData & """ );"
DoCmd.SetWarnings False
DoCmd.RunSQL (insSQL)
DoCmd.SetWarnings True
Response = acDataErrAdded
Case vbNo
Me.Undo
Response = acDataErrContinue
End Select

End Sub
 
Upvote 0
You have Option Explicit set, whcih means any variables used must be declared.

Just change:
Code:
insSQL = "INSERT INTO Project Managers( PM Name ) " & _ 
"VALUES ( """ & NewData & """ );"
to:
Code:
insSQL$ = "INSERT INTO Project Managers( PM Name ) " & _ 
"VALUES ( """ & NewData & """ );"

The little $ lets Access know that insSQL is a variable, and of the string variety.

You could also add:
Dim insSQL as string
or
Dim insSQL$
 
Upvote 0

Forum statistics

Threads
1,221,831
Messages
6,162,252
Members
451,757
Latest member
iours

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top