Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when using Combo Box to add new reocrd

Status
Not open for further replies.

MattLongmuir

Technical User
Oct 31, 2003
16
AU
Hi Team,
Here's the situation:

I have a table called "Company". Within is a p.key: CompanyID, and a text field: CompanyName

I have a form and a combo box. I intend to use the combo box, to pull a list of items from the Company table, but also allow users to enter in a new Company via the combo box.
Here's the code i'm using:

"Option Compare Database
Option Explicit

Private Sub cboCompanyID_NotInList(NewData As String, Response As Integer)
On Error GoTo err_cboCompanyID_NotInList

Dim ctl As Control
Dim strSQL As String

' Return Control object that points to combo box.
Set ctl = Me!cboCompanyID
' Prompt user to verify they wish to add new value.
If MsgBox("Company is not in list. Add it?", vbOKCancel) = vbOK Then
' Set Response argument to indicate that data is being added.
Response = acDataErrAdded
' Add string in NewData argument to Companys table.
NewData = CapitalizeFirst(NewData)
strSQL = " INSERT INTO Company ( CompanyName ) SELECT '" & Proper(NewData) & "'"
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
ctl.Value = NewData
DoCmd.SetWarnings True
Else
' If user chooses Cancel, suppress error message and undo changes.
Response = acDataErrContinue
ctl.Undo
End If

exit_cboCompanyID_NotInList:
Exit Sub

err_cboCompanyID_NotInList:
If Err = 2113 Then
Err = 0
Resume Next
Else
MsgBox Str(Err)
MsgBox Err.Description
Resume exit_cboCompanyID_NotInList
End If
End Sub"

Now, whenever i go to put in a new company, i get this message in Access2k's status bar:

"Control can't be edited; it's bound to AutoNumber field 'CompanyID'.

Argh, it's probably something simple, but i can't figure it out.

Can anyone help?
 
Nope, think i've figured that problem out.
Now i'm getting an error on this line:

Compile Error:
Sub or Function not defined.

hmmmmmmmm......
 
Ah fixed that now and all is working.
YAY!

I had an error in the CaptilizeFirst module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top