Solved

Should On Error Resume Next be used here ?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 56
Adding to a VBA? 6 69
fso.FolderExists("\\server\HiddenFolder$") 4 64
Sub or Function is not defined 6 26
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

808 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