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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb NotInList Event for Combo Box

Status
Not open for further replies.

DblDogDare

Technical User
Feb 11, 2003
9
0
0
US
I am having some difficuly with this NotInList event. My problem is; I am entering the new resource, then I tab to the next field. I get the message box to add the new resource. I enter the new resource and the name is added as a record in the table, but not in the list of the Combo Box and it keeps asking me to enter it again, so I have multiple entries of the same resource in the table. I have noticed that if I pick "No" the second time, it shows the new resource in the combo Box list. I think it is a problem with the requery of the combobox. Here is my code, please if someone can help, it would be greatly appreciated.

Private Sub cboResource_NotInList(NewData As String, Response As Integer)
vResponse = MsgBox("Data entered '" & NewData & "' is not contained in current list." & Chr(13) & "Do you wish to add it to the file?", 36, "New Data Prompt")
If vResponse = 6 Then
Dim MyDB As DAO.Database
Dim MyRS As DAO.Recordset
Set MyDB = CurrentDB
Set MyRS = MyDB.OpenRecordset("tblResources", dbOpenDynaset)
MyRS.AddNew
MyRS("DataField1") = NewData
MyRS("LastName") = Trim(Mid$(NewData,1,Instr(1,NewData,",")-1))
MyRS("FirstName") = Trim(Mid$(NewData, Instr(1,NewData,",")+1))
MyRS.Update
MyRS.Close
MyDB.Close
Response = DATA_ERRADDED
Else
Me![cboResource] = Null
Response = DATA_ERRCONTINUE
Me.cboResource.Requery
End If
End If
End Sub
 
It should look like this:
Code:
Private Sub cboResource_NotInList(NewData As String, Response As Integer)

    If (MsgBox("Data entered '" & NewData & "' is not contained in current list." & vbCrLf & "Do you wish to add it to the file?", vbQuestion + vbYesNo) = vbYes) Then
        
        Dim MyDB As DAO.Database
        Dim MyRS As DAO.Recordset
        
        Set MyDB = CurrentDb
        Set MyRS = MyDB.OpenRecordset("tblResources", dbOpenDynaset)
        
        MyRS.AddNew
        MyRS("DataField1") = NewData
        MyRS("LastName") = Trim(Mid$(NewData, 1, InStr(1, NewData, ",") - 1))
        MyRS("FirstName") = Trim(Mid$(NewData, InStr(1, NewData, ",") + 1))
        MyRS.Update
        MyRS.Close
        MyDB.Close
        
        Response = acDataErrAdded
  Else
        Me![cboResource].Undo
        Response = acDataErrContinue

  End If

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top