CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60

    Problem using "On Error" Statement...

    Hi all,

    On my form I have a text box(Text1) and a button(Command1), here is the code at the moment:

    Private Sub Command1_Click()
    On Error GoTo Err1

    If Int(Text1) < 0 Then
    MsgBox ("Only enter a positive number")
    Else
    MsgBox ("You have entered a correct digit")
    End If

    Err1: MsgBox ("An Error has occured")
    End Sub

    The reasons I have put in the error statement is to prevent the user from entering letters into the box. The problem is, if I enter a positive digit, e.g. 10 into the box, I get the expected message box appearing and then straight after I get the error box appearing??

    Any info on how I can fix this? Thanks.

    Mark

  2. #2
    Join Date
    Jan 2002
    Location
    Halifax, NS, Canada
    Posts
    985
    TRY

    If VAL(Text1) < 0 Then
    MsgBox ("Only enter a positive number")
    Else
    MsgBox ("You have entered a correct digit")
    End If

  3. #3
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60
    I tried this but you can still enter letters such as a,b,c an it will accept it.

    I want it to produce the error message when non-numerical characters are entered.

    Thanks anyway. Any more info...

    Mark

  4. #4
    Join Date
    Feb 2002
    Location
    TOULOUSE France
    Posts
    138

    Lightbulb Exit sub

    Modify your code as follow to prevent the execution of the msgbox when all is good

    Code:
    ...
    End If
    
    Exit Sub
    Err1: MsgBox ("An Error has occured")
    End Sub
    /.*/{Guru AWK}

  5. #5
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60
    Thanks, got it working fine now.

    Mark

  6. #6
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    You can also look for the CInt() function, and don't forget that Exit Sub (in a function, use Exit Function)

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured