Simple error handling --> AfterUpdate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PNY
    New Member
    • Mar 2007
    • 20

    Simple error handling --> AfterUpdate

    Hi all,

    I have a form that uses the code listed below on the after update of a combo box and list box to search for a selected Name.

    (code for afterupdate for combo box)
    Code:
    Private Sub Combo73_AfterUpdate()
      ' Find the record that matches the control.
        Me.RecordsetClone.FindFirst "[Name] = '" & Me![Combo73] & "'"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End Sub
    (code for afterupdate for list box)
    Code:
    Private Sub List75_AfterUpdate()
        ' Find the record that matches the control.
        Me.RecordsetClone.FindFirst "[Name] = '" & Me![List75] & "'"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End Sub
    How would I add the ability to display an error message if the user tries to search for a Name that doesn't exist in the records?
    Last edited by Scott Price; May 26 '08, 09:21 PM. Reason: Code tags
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Originally posted by PNY
    Hi all,

    I have a form that uses the code listed below on the after update of a combo box and list box to search for a selected Name.

    (code for afterupdate for combo box)
    Code:
    Private Sub Combo73_AfterUpdate()
      ' Find the record that matches the control.
        Me.RecordsetClone.FindFirst "[Name] = '" & Me![Combo73] & "'"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End Sub
    (code for afterupdate for list box)
    Code:
    Private Sub List75_AfterUpdate()
        ' Find the record that matches the control.
        Me.RecordsetClone.FindFirst "[Name] = '" & Me![List75] & "'"
        Me.Bookmark = Me.RecordsetClone.Bookmark
    End Sub
    How would I add the ability to display an error message if the user tries to search for a Name that doesn't exist in the records?
    [CODE=vb]
    Private Sub Combo73_AfterUp date()
    Dim strCriteria As String
    Dim rst As DAO.Recordset

    Set rst = Me.RecordsetClo ne

    strCriteria = "[Name] = '" & Me![Combo73] & "'"

    rst.FindFirst strCriteria

    If rst.NoMatch Then
    MsgBox "A Name of [" & UCase$(Me![Combo73]) & "] was not found in " & _
    "the Database!", vbExclamation, "Name not Found"
    Else
    Me.Bookmark = rst.Bookmark
    End If
    End Sub[/CODE]

    Comment

    • PNY
      New Member
      • Mar 2007
      • 20

      #3
      Originally posted by ADezii
      [CODE=vb]
      Private Sub Combo73_AfterUp date()
      Dim strCriteria As String
      Dim rst As DAO.Recordset

      Set rst = Me.RecordsetClo ne

      strCriteria = "[Name] = '" & Me![Combo73] & "'"

      rst.FindFirst strCriteria

      If rst.NoMatch Then
      MsgBox "A Name of [" & UCase$(Me![Combo73]) & "] was not found in " & _
      "the Database!", vbExclamation, "Name not Found"
      Else
      Me.Bookmark = rst.Bookmark
      End If
      End Sub[/CODE]
      Thanks ADezzi, it works like a charm! I tested it to see what would happen if the field was NULL and I get a runtime error ... How do I fix that?

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by PNY
        Thanks ADezzi, it works like a charm! I tested it to see what would happen if the field was NULL and I get a runtime error ... How do I fix that?
        Just add Line #5
        [CODE=vb]
        Private Sub Combo73_AfterUp date()
        Dim strCriteria As String
        Dim rst As DAO.Recordset

        If IsNull(Me![Combo73]) Then Exit Sub

        Set rst = Me.RecordsetClo ne

        strCriteria = "[LastName] = '" & Me![Combo73] & "'"

        rst.FindFirst strCriteria

        If rst.NoMatch Then
        MsgBox "A Name of [" & UCase$(Me![Combo73]) & "] was not found in " & _
        "the Database!", vbExclamation, "Name not Found"
        Else
        Me.Bookmark = rst.Bookmark
        End If
        End Sub[/CODE]

        Comment

        • PNY
          New Member
          • Mar 2007
          • 20

          #5
          Originally posted by ADezii
          Just add Line #5
          [CODE=vb]
          Private Sub Combo73_AfterUp date()
          Dim strCriteria As String
          Dim rst As DAO.Recordset

          If IsNull(Me![Combo73]) Then Exit Sub

          Set rst = Me.RecordsetClo ne

          strCriteria = "[LastName] = '" & Me![Combo73] & "'"

          rst.FindFirst strCriteria

          If rst.NoMatch Then
          MsgBox "A Name of [" & UCase$(Me![Combo73]) & "] was not found in " & _
          "the Database!", vbExclamation, "Name not Found"
          Else
          Me.Bookmark = rst.Bookmark
          End If
          End Sub[/CODE]

          Thanks so much for the help, ADezzi!!!

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by PNY
            Thanks so much for the help, ADezzi!!!
            Anytime, that's why we are all here.

            Comment

            • PNY
              New Member
              • Mar 2007
              • 20

              #7
              Originally posted by ADezii
              Anytime, that's why we are all here.
              Hi Again,

              I'm trying to do more error handling etc, and i'm wondering: Can there be a way to add a line in the above code to open a form if the Name is found? Does this make sense:
              Code:
              Private Sub Combo33_AfterUpdate()
              Dim strCriteria As String
              Dim rst As DAO.Recordset
               
              Set rst = Me.RecordsetClone
               
              strCriteria = "[Name] = '" & Me![Combo33] & "'"
               
              rst.FindFirst strCriteria
               
              If rst.NoMatch Then
                MsgBox "A Name of [" & UCase$(Me![Combo73]) & "] was not found in " & _
                       "the Database!", vbExclamation, "Name not Found"
              Else
                Me.Bookmark = rst.Bookmark
              	On Error GoTo Err_Command18_Click
              
                  		Dim stDocName As String
                 		Dim stLinkCriteria As String
              
                 	 stDocName = "fm_lookup_result"
                  
                  		stLinkCriteria = "[Prod_Spec]=" & "'" & Me![Combo31] & "'"
                 		DoCmd.OpenForm stDocName, , , stLinkCriteria
              
              Exit_Command18_Click:
                  Exit Sub
              
              Err_Command18_Click:
                  MsgBox Err.Description
                  Resume Exit_Command18_Click
              
              End If
              End Sub

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                [CODE=vb]
                Private Sub Combo73_AfterUp date()
                On Error GoTo Err_Combo73_Aft erUpdate
                Dim strCriteria As String
                Dim rst As DAO.Recordset
                Dim stDocName As String
                Dim stLinkCriteria As String

                stDocName = "fm_lookup_resu lt"
                stLinkCriteria = "[Prod_Spec]=" & "'" & Me![Combo31] & "'"

                If IsNull(Me![Combo73]) Then Exit Sub

                Set rst = Me.RecordsetClo ne

                strCriteria = "[LastName] = '" & Me![Combo73] & "'"

                rst.FindFirst strCriteria

                If rst.NoMatch Then
                MsgBox "A Name of [" & UCase$(Me![Combo73]) & "] was not found in " & _
                "the Database!", vbExclamation, "Name not Found"
                Else
                'Guess you won't be needing this, now
                'Me.Bookmark = rst.Bookmark
                DoCmd.OpenForm stDocName, , , stLinkCriteria
                End If

                Exit_Combo73_Af terUpdate:
                Exit Sub

                Err_Combo73_Aft erUpdate:
                MsgBox Err.Description , vbExclamation, "Error in Combo73_AfterUp date()"
                Resume Exit_Combo73_Af terUpdate
                End Sub[/CODE]

                Comment

                Working...