Solved

Should On Error Resume Next be used here ?

Posted on 2014-11-22
5
125 Views
Last Modified: 2014-11-22
This function validates a users input, just noticed that is can produce a type mismatch error.


valid input  yjk50kg01dky


Function ValidateInPut(sString As String) As Boolean
          Dim str1 As String
          Dim intNum As Long
          Dim str2 As String
          Dim LowValue As Long
          Dim HighValue As String
10        On Error GoTo e
20        LowValue = 5000
30        HighValue = 10000
40        str1 = Mid$(sString, 4, 2)
50        str2 = Mid$(sString, 8, 2)
60        intNum = str1 & str2
         If intNum >= LowValue And intNum <= HighValue Then
80         ValidateInPut = True
90        Else
100         ValidateInPut = False
110       End If
120    Exit Function

e:
130          SendToSupport gWindowsVersion, Me.name, "Function ValidateInPut(sString As String)", Erl, err.Number, err.Description 'send thru jmail 01-26-2011
140          ShowError err.Number, err.Description, "Function ValidateInPut(sString As String)", Erl 'write to file and display error


End Function
these produce a type mismatch error if a letter is typed where a number is expected
str1 = Mid$(sString, 4, 2)
str2 = Mid$(sString, 8, 2)

should i change the error handler to On Error Resume Next or ?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40459444
when I was doing VB6, I had a strict rule: "On Error Resume Next" shall only be used in very specific scenarios where an error on a single line was expected on not trappable by any other mechanism.

In this case here, you can check if the lenght of sString is at least 10. Then you chan check if str1 and str2 are numeric.
0
 

Author Closing Comment

by:isnoend2001
ID: 40459465
Thanks
0
 

Author Comment

by:isnoend2001
ID: 40459500
Thanks Éric Moreau
I have decided to do this:
Function ValidateInPut(sString As String) As Boolean
          Dim str1 As String
          Dim intNum As Long
          Dim str2 As String
          Dim LowValue As Long
          Dim HighValue As String
10        On Error GoTo e
20        LowValue = 5000
30        HighValue = 10000
40        str1 = Mid$(sString, 4, 2)
50        str2 = Mid$(sString, 8, 2)

60        intNum = str1 & str2
         If intNum >= LowValue And intNum <= HighValue Then
80         ValidateInPut = True
90        Else
100         ValidateInPut = False
            goto e
110       End If
120    Exit Function

e:
ValidateInPut = False ' send attempted hack by email
         SendToSupport "Invalid Input: " & sString & " " & gWindowsVersion, Me.name, "Function ValidateInPut(sString As String)", Erl, err.Number, err.Description 'send thru jmail 01-26-2011
'Removed error message
Exit Function

End Function
This should send the invalid entry by email and not display error
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40459513
what if SendToSupport fails? Is this method correctly supports error handling?
0
 

Author Comment

by:isnoend2001
ID: 40459535
Good thought
It does check for internet connection
If Not CheckInternetConnection Then GoTo e
it does not display an error it just does not send the info
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question