Solved

Should On Error Resume Next be used here ?

Posted on 2014-11-22
5
127 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

688 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