I'm banging my head against this one.
I have a combobox that allows users to select a volunteer's name from list. They may add a new volunteer by typing in first name and last name, which is the only thing we need to know about the volunteers, and the combobox get split into two corresponding fields for table tblVolunteer.
The code works *perfectly*, yet Access fires a default "You cannot add this to list" warning. I've tried several ways of juggling it to get it disabled or to requery the table (even though it isn't necessary) to no avail.
However, using RuralGuy's diagnostic tool as below
I can tell you that none of bajillion commands I've tried will actually ensure that the NewData is updated to the table actually does update it.
However, when I omit out the parts telling to split the NewData into two components, the code works perfectly and this time without any warning from Access. Put it back in, and it fires a warning. Either way, combobox has a new record added and automatically inserted.
Here is the code, san Rural Guy's diagnostic tool for clarity:
Any suggestion would be greatly appreciated.
I have a combobox that allows users to select a volunteer's name from list. They may add a new volunteer by typing in first name and last name, which is the only thing we need to know about the volunteers, and the combobox get split into two corresponding fields for table tblVolunteer.
The code works *perfectly*, yet Access fires a default "You cannot add this to list" warning. I've tried several ways of juggling it to get it disabled or to requery the table (even though it isn't necessary) to no avail.
However, using RuralGuy's diagnostic tool as below
Code:
If IsNull(DLookup("FirstName", "tblVolunteers", _
"[FirstName] = '" & NewData & "'")) Then
MsgBox "[" & NewData & "] is NOT in the Table yet!"
Else
MsgBox "[" & NewData & "] IS in the Table!"
End If
I can tell you that none of bajillion commands I've tried will actually ensure that the NewData is updated to the table actually does update it.
However, when I omit out the parts telling to split the NewData into two components, the code works perfectly and this time without any warning from Access. Put it back in, and it fires a warning. Either way, combobox has a new record added and automatically inserted.
Here is the code, san Rural Guy's diagnostic tool for clarity:
Code:
Private Sub cmbVolunteerID_NotInList(NewData As String, Response As Integer)
'The code is provided by SJ McAbney, from access-programmers.co.uk/forums
'It can be found at http://www.access-programmers.co.uk/forums/showthread.php?t=98091
' provide text constants to reduce text later and allow for faster execution
' due to added speed from the compilation of constants
Const Message1 = "The data you have entered is not in the current selection."
Const Message2 = "Would you like to add it?"
Const Title = "Unknown entry."
Const NL = vbCrLf & vbCrLf
' database and recordset object variables
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim Splitter As Variant
Splitter = Array(2, 1)
' show message box and evaluate if the user has selected Yes or No
If MsgBox(Message1 & NL & Message2, vbQuestion + vbYesNo, Title) = vbYes Then
' open a connection to the current database
Set db = CurrentDb
Set rs = db.OpenRecordset("tblVolunteers")
' using the recordset object
'creates an array of last name and first name
Splitter = (Split([NewData], " "))
With rs
.AddNew ' prepare to add a new record
.Fields("FirstName") = Splitter(0)
.Fields("LastName") = Splitter(1) ' add unfound data into field
.Update ' update the table
.Close ' close the recordset object
End With
Response = acDataErrAdded ' confirm record added
Else
Me.cmbVolunteerID.Undo ' clear the entry in the combobox
Response = acDataErrContinue ' confirm the record is not allowed
End If
End Sub
Any suggestion would be greatly appreciated.