Error Handling/Trapping Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JodiPhillips
    New Member
    • May 2007
    • 26

    Error Handling/Trapping Question

    Hello again,

    Its been a while since I've asked a question, I've been learning steadily reading the How To's and the questions in this forum, as well as reading my Access VBA and Access bibles till they are now in tatters!.

    I'm stuck with understanding the error handling process by VB6.5 (in particular for MSAccess 2003).

    The following code is a test code only, so that I could understand things before I go on to something more meaty. It is probably the 5th incarnation or attempt and as you can see I have resorted to If..Then..Else and a SubFunction to try and handle this type of error. The error I'm referring to is the nasy Err 13 Data Type Mismatch error which occurs when I deliberately enter a non numeric character in the text box on the form. (The from has one command button and one text box)

    Code:
    Sub Command0_Click()
    
                Dim aNumberbox As Double
            Dim NotANumber As Variant
                            aNumberbox = 0
               NotANumber = CVErr(13)
         
         aNumberbox = CheckData(Me.Text1)
          
          If IsError(aNumberbox) Then
             Select Case aNumberbox
                    Case NotANumber
                   MsgBox "Error: not a number."
                Case Else
                   MsgBox "Unknown Error."
             End Select
          Else
          aNumberbox = Me.Text1
              MsgBox ("You entered the number " & aNumberbox)
              MsgBox ("Why not try it again?")
          End If
       End Sub
    
       Function CheckData(aNumberbox As Integer) As Integer
         Dim NotANumber As Integer
         
          If Not IsNumeric(aNumberbox) Then
             CheckData = NotANumber
                End If
       End Function
    Despite trying to handle the error with a very simple error handle subroutine in my previous attempt:

    Code:
    Sub Command0_Click()
    
    Dim aNumber As Double
    On Error GoTo ErrorHandle
    
    aNumber = CDbl(Me.Text1)
    ExitHere:
      MsgBox ("You entered the number " & aNumber)
     MsgBox ("Why not try it again?")
      
      
    ErrorHandle:
      MsgBox ("Please enter a number.")
      Resume ExitHere
    End Sub
    No matter what I do the Built-In Error Message (as unhelpful as they are :P) still occur.

    Both sets of code of course break at the "aNumber = CDbl(Me.Text1)" line.

    Would really appreciate it if anyone can help with this :)

    JP
  • anuragshrivastava64
    New Member
    • Jan 2007
    • 66

    #2
    Sorry but can't get what u want exactly
    anyways a dirty solution is here

    Sub Command0_Click( )

    Dim aNumber As Double
    On Error GoTo ErrorHandle

    aNumber = CDbl(Me.Text1)
    ExitHere:
    MsgBox ("You entered the number " & aNumber)
    MsgBox ("Why not try it again?")
    Me.Text1.SetFoc us
    Exit Sub

    ErrorHandle:
    MsgBox ("Please enter a number.")
    Resume ExitHere
    End Sub

    Comment

    • JodiPhillips
      New Member
      • May 2007
      • 26

      #3
      Originally posted by anuragshrivasta va64
      Sorry but can't get what u want exactly
      anyways a dirty solution is here

      Sub Command0_Click( )

      Dim aNumber As Double
      On Error GoTo ErrorHandle

      aNumber = CDbl(Me.Text1)
      ExitHere:
      MsgBox ("You entered the number " & aNumber)
      MsgBox ("Why not try it again?")
      Me.Text1.SetFoc us
      Exit Sub

      ErrorHandle:
      MsgBox ("Please enter a number.")
      Resume ExitHere
      End Sub

      Hello anuragshrivasta va

      Thank you for your reply.

      What I am trying to achieve is to stop the built-in error handling from occuring when the code encounters an error. The variable aNumber is set as a double to capture all digits (both whole and decimal) and the code will work fine with a number entered. However, enter a text character and the built-in error handler returns a type mismatch error, which is expected due to the type of data entered not being numeric.

      I'm trying to stop the in-built mismatch error from reporting, and the mismatch error to be handled specifically by the ErrorHandle code block.

      Can the mismatch type error be trapped and handled programmaticall y or will I have to respecify the data type of the anumber variable as perhaps a variant? If I have to change the data type to variant I will need to go with something similar to the first code block posted above using If...then...els e (which still doesn't work to stop the in-built error handling).

      Thanks again.

      JP

      Comment

      • anuragshrivastava64
        New Member
        • Jan 2007
        • 66

        #4
        Originally posted by JodiPhillips
        Hello anuragshrivasta va

        Thank you for your reply.

        What I am trying to achieve is to stop the built-in error handling from occuring when the code encounters an error. The variable aNumber is set as a double to capture all digits (both whole and decimal) and the code will work fine with a number entered. However, enter a text character and the built-in error handler returns a type mismatch error, which is expected due to the type of data entered not being numeric.

        I'm trying to stop the in-built mismatch error from reporting, and the mismatch error to be handled specifically by the ErrorHandle code block.

        Can the mismatch type error be trapped and handled programmaticall y or will I have to respecify the data type of the anumber variable as perhaps a variant? If I have to change the data type to variant I will need to go with something similar to the first code block posted above using If...then...els e (which still doesn't work to stop the in-built error handling).

        Thanks again.

        JP
        Then u can simply check whetetehr the text enterd is numeric or not thru ISNumeric function

        Just copy the following spurce of code before u chek

        If Not IsNumeric(Me.Te xt1) Then

        MsgBox "Sorry wrong entry"
        Text1= ""
        Text1.setfocus
        Else
        aNumberbox = CheckData(Me.Te xt1)
        End If

        Comment

        • JodiPhillips
          New Member
          • May 2007
          • 26

          #5
          Hello again anuragshrivasta va, :)

          Thank you again for replying.

          Thanks also for the code tweak. I've probably not asked the question correctly in the first place. I'm actually looking not for an actual solution but an understanding of how to deal with errrors within VB in a very general sense. The code at the top is just a test code to see how the whole error trapping and handling event is carried out, and to show that I have at least tried to do this on my own. I picked the dreaded error 13 data type mismatch to test as it seems to be the one that will most often cause issues if a user doesn't understand or makes an error with data entry. I guess where I've got myself very confused is from the different methodologies of error handling between different versions of VB (2003 - 2005 for instance) and looking at things like C++ etc.

          I do appreciate you taking the time to respond and I apologise if I have wasted your time.

          Comment

          • anuragshrivastava64
            New Member
            • Jan 2007
            • 66

            #6
            I am afraid I can't help u thru the whole exception handler methodology in VB.

            Anyways what u can do is to create a new exception handler and may be write the following

            ExceptionHandle r:
            If Err.Number =13
            MsgBox "Please enter the number correctly"
            End if

            If anything else u want to know please feel free to ask.

            Comment

            Working...