Okay, after this I'll shut up...
I have a lovely piece of code to change the underlying table of a combo box, culled from the faq's here and another site.
I have a couple of combo boxes so I want them both to use this code so I created a public Function that returns the response for the On Not In List Event. Here it is:
Public Function Add2Combo(fieldName As field, rs As Recordset, NewData As String) As Integer
Dim strData, qts As String
qts = Chr(34)
strData = Trim$(NewData)
If MsgBox(qts & strData & qts & " does not appear in the database." _
& Chr(13) & "Do you want to add it now?", 36) = vbNo Then
Add2Combo = acDataErrContinue
Else
On Error Resume Next
rs.AddNew
rs!fieldName = strData
rs.Update
If Err Then
MsgBox "An error occurred. Please try again."
Add2Combo = acDataErrContinue
Else
Add2Combo = acDataErrAdded
End If
End If
End Function
I get the following error when it's run:
Item not found in this collection. And the "rs!fieldName = strData" line highlighted.
I've tried just putting in rs![Course Type] = strData and this works fine and as far as I know, this is what I'mm passing to the function in the firstplace.
Here's the procedure that calls this function
Private Sub Course_Type_NotInList(NewData As String, Response As Integer)
Dim db As Database, rs As Recordset, fieldName As field
Set db = CurrentDb
Set rs = db.OpenRecordset("tblCourseTypeLookup", dbOpenDynaset)
Set fieldName = rs![Course Type]
Response = Add2Combo(fieldName, rs, NewData)
Set db = Nothing
rs.Close
Set rs = Nothing
End Sub
Can anyone shed any light on what I promise is the last you'll hear (from me) about combo boxes?
Cheers,
Pete
I have a lovely piece of code to change the underlying table of a combo box, culled from the faq's here and another site.
I have a couple of combo boxes so I want them both to use this code so I created a public Function that returns the response for the On Not In List Event. Here it is:
Public Function Add2Combo(fieldName As field, rs As Recordset, NewData As String) As Integer
Dim strData, qts As String
qts = Chr(34)
strData = Trim$(NewData)
If MsgBox(qts & strData & qts & " does not appear in the database." _
& Chr(13) & "Do you want to add it now?", 36) = vbNo Then
Add2Combo = acDataErrContinue
Else
On Error Resume Next
rs.AddNew
rs!fieldName = strData
rs.Update
If Err Then
MsgBox "An error occurred. Please try again."
Add2Combo = acDataErrContinue
Else
Add2Combo = acDataErrAdded
End If
End If
End Function
I get the following error when it's run:
Item not found in this collection. And the "rs!fieldName = strData" line highlighted.
I've tried just putting in rs![Course Type] = strData and this works fine and as far as I know, this is what I'mm passing to the function in the firstplace.
Here's the procedure that calls this function
Private Sub Course_Type_NotInList(NewData As String, Response As Integer)
Dim db As Database, rs As Recordset, fieldName As field
Set db = CurrentDb
Set rs = db.OpenRecordset("tblCourseTypeLookup", dbOpenDynaset)
Set fieldName = rs![Course Type]
Response = Add2Combo(fieldName, rs, NewData)
Set db = Nothing
rs.Close
Set rs = Nothing
End Sub
Can anyone shed any light on what I promise is the last you'll hear (from me) about combo boxes?
Cheers,
Pete