Solved

Should On Error Resume Next be used here ?

Posted on 2014-11-22
5
116 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
  • 3
  • 2
5 Comments
 
LVL 69

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 82
VBA/SQL - Connect to SQL server and pull data 4 88
How does CurrentUser work? 10 31
Exit a vb6 apps when a calling it apps closes 15 43
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now