Error handle

Posted on 1999-07-04
Medium Priority
Last Modified: 2010-05-02
Since there are more than three Textboxes in my application needed the user input number from 0
to 9999, I want a function which could deal with
the input errors, and give me an example to tell
how to call the function. Thanks!
Question by:leeann

Accepted Solution

marscpa earned 200 total points
ID: 1521837
This is how I would write a quick function:

Private Function ValInput(sInput As String) As Boolean
    If IsNumeric(sInput) = True Then
        If Val(sInput) >= 0 And Val(sInput) < 10000 Then
            ValInput = True
            ValInput = False
        End If
        ValInput = False
    End If
End Function

This example doesn't let the user leave the text box if the input doesn't pass validation. I'd probably add a msg box.

Private Sub Text1_LostFocus()
    If ValInput(Text1.Text) = False Then
        Text1.SelStart = 0
        Text1.SelLength = Len(Text1.Text)
    End If
End Sub

The IF statement here demonstrates calling the function.


Expert Comment

ID: 1521838
I would do it even shorter:

Private Function ValInput(sInput As String) As Boolean

If Not IsNumeric(sInput) = True Then exit function
If Val(sInput) < 0 And _
                 Val(sInput) > 10000 Then Exit Function
ValInput = True
End Function


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

624 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