"Not in list" event for Access 2007

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vanlanjl
    New Member
    • Feb 2009
    • 46

    "Not in list" event for Access 2007

    I cannot get the not in list event to work right.

    On my form I have a combo box named "cboChargeC ode"

    I have a table named "tblChargeC ode"

    I would like it so when you enter a value into the cbohargeCOde that is not listed it will alert you and ask you if you would like to add such item.

    My code
    Code:
    Private Sub ChargeCode_NotInList(NewData As String, Response As Integer)
    Dim strSQL As String
    Dim i As interger
    Dim MSg As String
    
    'Exit this sub if the combo box is cleared
    If NewData = "" Then Exit Sub
    
    MSg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
    MSg = MSg & "Do you want to add it?"
    
    i = MSgBox(MSg, vbQuestion + vbYesNo, "Unknown Charge Code...")
    If i = vbYes Then
        strsql = "Insert into tblChargeCode ([strChargeCode])" & CurrentDb.excute strSQL, dbFailOnError
        Response = acDataErrAdded
    Else
        Response = acDataErrContinue
    End If
    error occurs on line:
    Code:
    strsql = "Insert into tblChargeCode ([strChargeCode])" & CurrentDb.excute strSQL, dbFailOnError
    I got this code form Microsoft so...
    not sure what is going on or why it doesnt work, any help would be awesome!
    Thanks
  • RuralGuy
    Recognized Expert Contributor
    • Oct 2006
    • 375

    #2
    Recheck where you got the code from (Microsoft). You'll see it is not the same.

    Comment

    • vanlanjl
      New Member
      • Feb 2009
      • 46

      #3
      I figured it out.
      Code:
      Option Compare Database
      
      Private Sub cboChargeCode_NotInList(NewData As String, Response As Integer)
      Dim strSQL As String
      Dim i As Integer
      Dim MSg As String
      
      'Exit this sub if the combo box is cleared
      If NewData = "" Then Exit Sub
      
      MSg = "'" & NewData & "' is not currently in the list." & vbCr & vbCr
      MSg = MSg & "Do you want to add it?"
      
      i = MsgBox(MSg, vbQuestion + vbYesNo, "Unknown Charge Code...")
      If i = vbYes Then
          strSQL = "Insert into tblChargeCode ([ChargeCode])" & " VALUES ('" & NewData & "')"
          CurrentDb.Execute strSQL, dbFail
          Response = acDataErrAdded
      Else
          Response = acDataErrContinue
      End If
      End Sub

      Comment

      • RuralGuy
        Recognized Expert Contributor
        • Oct 2006
        • 375

        #4
        Excellent! Glad you got it sorted.

        Comment

        • 92Felton
          New Member
          • Dec 2011
          • 1

          #5
          Hey I'm Felton and here for the first time. I came across this thread and I find It truly useful & it helped me out much. I hope to give something back and aid others like you aided me.

          Comment

          • RuralGuy
            Recognized Expert Contributor
            • Oct 2006
            • 375

            #6
            Welcome. Glad we could help.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Suspicion alert:
              Why would this help anyone but the OP? It's not like it's a common mistake to make, and it's all about the OP's mistake.

              If 92Felton goes on to post normally then no problem. If, on the other hand, they post anything suspicious, then this should not be taken to indicate their motives are pure.

              Comment

              Working...