Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
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

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

715 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