Solved

Should On Error Resume Next be used here ?

Posted on 2014-11-22
5
112 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
Comment Utility
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
Comment Utility
Thanks
0
 

Author Comment

by:isnoend2001
Comment Utility
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
Comment Utility
what if SendToSupport fails? Is this method correctly supports error handling?
0
 

Author Comment

by:isnoend2001
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now