K
Klingon Coder
Hi,
I am hoping one of you gurus may be able to help me out.
I have a combo box on a Task form that brings back the ContactID from the
Contact table, but displays a concatenation of the last name a comma and the
first name (i.e. Doe, John). The user is able to enter the Contact name as
either “John Doe†or “Doe, Johnâ€. There is code in place that will take the
user’s entered data and parse the last name and the first name regardless of
how they entered it. A comparison of the last name and first name is then
made against those in the Contact table and if the user’s information matches
the proper name is selected. The only difference in the behaviour is if they
entered Doe, John, the list jumps to the first name that matches the text
being typed, i.e. typing the D will bring the user to the first Name
beginning with D etc. If they enter John Doe when they attempt to leave the
combo box the name Doe, John will be highlighted with the list part of the
combo box being open. As near as I can figure out, this would be the normal
way for it to behave and I don’t have any issue with the user having to tab
twice to get out of the combo box. The situation I am having trouble with is
when the contact is not in the list.
I have the combo box Limit To List property set to yes and if a user enters
a value that isn’t in the list a message box opens asking if they want to add
the contact to the list. On an affirmative reply, the Contact form opens and
inserts the names that they had typed on the Task form into their respective
text boxes. Everything works flawlessly as far as triggering the NotInList
event, the Contact form opens and the proper fields are filled based on the
information that was passed from the Task form. The user can save the record
and close the form, but here is where things get hairy. If the user entered
the Contact name as “Doe, John†on the Task form, then the new Contact
information is brought back and fills the combo box as it should and they can
proceed with the rest of their data. However, if the user entered “John Doeâ€
into the combo box, the NotInList event fires and as I mentioned the
information is inserted into the appropriate text boxes, but when the user
saves the data and closes the form they get the following message box:
“The text you entered isn’t an item in the list.
Select an item from the list, or enter text that matches one of the listed
items.â€
When the user clicks the corresponding OK button the combo box list opens
and the new data is shown in the list (not necessarily highlighted, but it is
in the list). I am not sure why the combo box isn’t having the new Contact
information displayed in this circumstance. It would appear that it is
comparing the First Name to the Last Name and getting a mismatch, but what
doesn’t make sense is why it can find the Contact when the name is already in
the list, but not when it has just been added.
I have enclosed the NotInList code in hopes someone might be able to point
out what I am missing. Thanks.
Private Sub cboContactID_NotInList(NewData As String, Response As Integer)
Dim db As Database
Dim rs As Recordset
Dim lngContactID As Long
Dim strLast As String
Dim strFirst As String
Dim varContact As Variant
If InStr(1, NewData, ",") > 0 Then
'Comma Last, First Name
strLast = ParseLast(NewData)
strFirst = ParseFirst(NewData)
ElseIf InStr(1, NewData, " ") > 0 Then
strLast = RParse(NewData, , " ")
strFirst = Parse(NewData, , " ")
Else
' no comma First Last Name
strLast = NewData
End If
strLast = SetUpper(strLast)
strFirst = SetUpper(strFirst)
'dlookup against table and set name values
strsearch = "[lastname] = " & "'" & strLast & "' and " & "[firstname]= "
& "'" & strFirst & "'"
varContact = DLookup("[ContactID]", "tblContact", strsearch)
If IsNull(varContact) Then ' not in list either way
If vbYes = MsgBox("'" & NewData & "' is not a current Contact." &
vbCrLf & "Do you wish to add it?", vbQuestion + vbYesNo, "Add Contact") Then
Set db = DBEngine(0)(0)
Set rs = db.OpenRecordset("SELECT * FROM [tblContact] WHERE 1=2;")
With rs
.AddNew
![LastName] = strLast
![FirstName] = strFirst
lngContactID = ![ContactID]
![ContactType] = [ContactType]
.Update
End With
rs.Close
Set rs = Nothing
Set db = Nothing
DoCmd.OpenForm "frmContact", , , "[ContactID]=" & lngContactID,
acFormEdit, acDialog, 1
Response = acDataErrAdded
Me.cboContactID = varContact
Else
Response = acDataErrContinue
End If
Else
Response = acDataErrContinue
Me.cboContactID = varContact
Exit Sub
End If
Me.cboContactID.Value = lngContactID
NewData = Me.cboContactID.Text
End Sub
--
Cheers
Klingon Coder
"Ancient Klingon Proverb - Act and you shall have dinner, Think and you
shall be dinner"
I am hoping one of you gurus may be able to help me out.
I have a combo box on a Task form that brings back the ContactID from the
Contact table, but displays a concatenation of the last name a comma and the
first name (i.e. Doe, John). The user is able to enter the Contact name as
either “John Doe†or “Doe, Johnâ€. There is code in place that will take the
user’s entered data and parse the last name and the first name regardless of
how they entered it. A comparison of the last name and first name is then
made against those in the Contact table and if the user’s information matches
the proper name is selected. The only difference in the behaviour is if they
entered Doe, John, the list jumps to the first name that matches the text
being typed, i.e. typing the D will bring the user to the first Name
beginning with D etc. If they enter John Doe when they attempt to leave the
combo box the name Doe, John will be highlighted with the list part of the
combo box being open. As near as I can figure out, this would be the normal
way for it to behave and I don’t have any issue with the user having to tab
twice to get out of the combo box. The situation I am having trouble with is
when the contact is not in the list.
I have the combo box Limit To List property set to yes and if a user enters
a value that isn’t in the list a message box opens asking if they want to add
the contact to the list. On an affirmative reply, the Contact form opens and
inserts the names that they had typed on the Task form into their respective
text boxes. Everything works flawlessly as far as triggering the NotInList
event, the Contact form opens and the proper fields are filled based on the
information that was passed from the Task form. The user can save the record
and close the form, but here is where things get hairy. If the user entered
the Contact name as “Doe, John†on the Task form, then the new Contact
information is brought back and fills the combo box as it should and they can
proceed with the rest of their data. However, if the user entered “John Doeâ€
into the combo box, the NotInList event fires and as I mentioned the
information is inserted into the appropriate text boxes, but when the user
saves the data and closes the form they get the following message box:
“The text you entered isn’t an item in the list.
Select an item from the list, or enter text that matches one of the listed
items.â€
When the user clicks the corresponding OK button the combo box list opens
and the new data is shown in the list (not necessarily highlighted, but it is
in the list). I am not sure why the combo box isn’t having the new Contact
information displayed in this circumstance. It would appear that it is
comparing the First Name to the Last Name and getting a mismatch, but what
doesn’t make sense is why it can find the Contact when the name is already in
the list, but not when it has just been added.
I have enclosed the NotInList code in hopes someone might be able to point
out what I am missing. Thanks.
Private Sub cboContactID_NotInList(NewData As String, Response As Integer)
Dim db As Database
Dim rs As Recordset
Dim lngContactID As Long
Dim strLast As String
Dim strFirst As String
Dim varContact As Variant
If InStr(1, NewData, ",") > 0 Then
'Comma Last, First Name
strLast = ParseLast(NewData)
strFirst = ParseFirst(NewData)
ElseIf InStr(1, NewData, " ") > 0 Then
strLast = RParse(NewData, , " ")
strFirst = Parse(NewData, , " ")
Else
' no comma First Last Name
strLast = NewData
End If
strLast = SetUpper(strLast)
strFirst = SetUpper(strFirst)
'dlookup against table and set name values
strsearch = "[lastname] = " & "'" & strLast & "' and " & "[firstname]= "
& "'" & strFirst & "'"
varContact = DLookup("[ContactID]", "tblContact", strsearch)
If IsNull(varContact) Then ' not in list either way
If vbYes = MsgBox("'" & NewData & "' is not a current Contact." &
vbCrLf & "Do you wish to add it?", vbQuestion + vbYesNo, "Add Contact") Then
Set db = DBEngine(0)(0)
Set rs = db.OpenRecordset("SELECT * FROM [tblContact] WHERE 1=2;")
With rs
.AddNew
![LastName] = strLast
![FirstName] = strFirst
lngContactID = ![ContactID]
![ContactType] = [ContactType]
.Update
End With
rs.Close
Set rs = Nothing
Set db = Nothing
DoCmd.OpenForm "frmContact", , , "[ContactID]=" & lngContactID,
acFormEdit, acDialog, 1
Response = acDataErrAdded
Me.cboContactID = varContact
Else
Response = acDataErrContinue
End If
Else
Response = acDataErrContinue
Me.cboContactID = varContact
Exit Sub
End If
Me.cboContactID.Value = lngContactID
NewData = Me.cboContactID.Text
End Sub
--
Cheers
Klingon Coder
"Ancient Klingon Proverb - Act and you shall have dinner, Think and you
shall be dinner"