NotInList Warning

M

m stroup

I have marked Limit to List as yes. I have entered the following code:

Private Sub cboCompany_NotInList(NewData As String, Response As Integer)
On Error GoTo Err_cboCompany_NotInList

Dim NewCompany As String
Dim db As DAO.Database
Dim rs As DAO.Recordset

If MsgBox("Would you like to add this company?") = vbNo Then
MsgBox ("Please enter a valid company from the drop down box.")
Else
'Open a new recordset in the Company table.
NewCompany = cboCompany.Text
Set db = CurrentDb
Set rs = db.OpenRecordset("tblCompanies", dbOpenDynaset)
rs.AddNew
rs![Company] = NewCompany
rs.Update

'Set cboCompany to NewCompany
cboCompany.Value = NewCompany
End If

Exit_cboCompany_NotInList:
Exit Sub

Err_cboCompany_NotInList:
MsgBox Err.Description

End Sub

I continue to get the 'the item is not on the list' message. Any suggestions?
 
D

Douglas J. Steele

You forgot to set the Response variable to acDataErrContinue when they
select No from the message box, or acDataErrAdded when they don't select No.

Of course, I don't believe your If statement can work the way you want,
since it doesn't give them a Yes/No option! Try

If MsgBox("Would you like to add this company?", vbYesNo) = vbNo Then

or, to be a little fancier,

If MsgBox("Would you like to add this company?", _
vbYesNo + vbQuestion) = vbNo Then
 
R

Roger Carlson

There's two parts to this.

First of all, you need to add
Response = acDataErrContinue
in your IF clause and
Response = acDataErrAdded
in your ELSE clause

On my website (www.rogersaccesslibrary.com), is a small Access database
sample called "NotInList.mdb" which illustrates this and some other
alternatives.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
S

Stuart McCall

m stroup said:
I have marked Limit to List as yes. I have entered the following code:

Private Sub cboCompany_NotInList(NewData As String, Response As Integer)
On Error GoTo Err_cboCompany_NotInList

Dim NewCompany As String
Dim db As DAO.Database
Dim rs As DAO.Recordset

If MsgBox("Would you like to add this company?") = vbNo Then
MsgBox ("Please enter a valid company from the drop down box.")
Else
'Open a new recordset in the Company table.
NewCompany = cboCompany.Text
Set db = CurrentDb
Set rs = db.OpenRecordset("tblCompanies", dbOpenDynaset)
rs.AddNew
rs![Company] = NewCompany
rs.Update

'Set cboCompany to NewCompany
cboCompany.Value = NewCompany
End If

Exit_cboCompany_NotInList:
Exit Sub

Err_cboCompany_NotInList:
MsgBox Err.Description

End Sub

I continue to get the 'the item is not on the list' message. Any
suggestions?

At the end of your Else block, add the line:

Response = acDataErrAdded

Also, for completeness, add the this line after the MsgBox line:

Response = acDataErrContinue

The technique is well covered in the help topic 'NotInList Event'.
 
M

m stroup

Thanks, Doug. You are right. I only get an ok message box. I had caught
the response piece but not the incorrect code for the box!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

NotInList not firing ? 1
NotInList Problem 3
NotinList problem 7
Not In List 2 values 5
Not in list requery issue 4
NotInList Errors 3
NOTINLIST 2
Combo Box Adding to List--Error 91 2

Top