Adblock breaks this site

How do I make a msg box come up on an error?

Discussion in 'Programming General' started by Visual Basic Matt, Jul 7, 2008.

  1. Visual Basic Matt

    Visual Basic Matt Apprentice

    Joined:
    Jan 29, 2008
    Posts:
    647
    Referrals:
    2
    Sythe Gold:
    56
    Discord Unique ID:
    223154494878253056
    How do I make a msg box come up on an error?

    Ok, I am working on hiscore lookup. It work's fine. The problem is, is that when you type in an invalid username. (Does not feature in the hiscores) You get the error,
    Code:
    Run-time error '9':
    Subscript out of range 
    I don't need to fix the error just need to make a msgbox come up (Username) Does not feature in the high scores. So basicly instead of the vb error box,
    Code:
    Run-time error '9':
    Subscript out of range 
    A msg box
    Code:
    MsgBox (txtUsername.Text) + " does not feature in the hiscores!", vbCritical = vbOKOnly, "Oops!": Exit Sub
    I am almost 100% sure that their is somthing like,
    Code:
    Private sub cmdLookup()
    If txtUsername.Text = "" Then
    MsgBox "You can't leave the username feild empty!"
    Else
        lstHighscores.Clear
        getHighscores txtUsername.Text
    End if
    on error (9) goto featureerror
    End sub
    Public Sub featureerror()
    MsgBox (txtUsername.Text) + " does not feature in the hiscores!", vbCritical = vbOKOnly, "Oops!": Exit Sub
    End Sub
    Thank's for all your help :)
     
  2. Covey

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816
    How do I make a msg box come up on an error?

    To be honest, you should fix the error. Just working around errors is sloppy work (i do it myself sometimes out of laziness).

    But to make an errorhandler this is how you do it:
    Code:
    Private Sub Command1_Click()
      On Error GoTo errorhandler
    
      'all your code goes here
    
      Exit Sub 'If it is a function you must put "Exit Function" instead.
    errorhandler:
      MsgBox "An error has occurred!" & vbnewline & "Error Number: " & err.number & vbnewline & "Error Description: " & err.description,vbexclamation,"Error!"
      Resume Next 'This peice of code will resume the next line of code after the error has occurred. In your case its probably better to leave it out.
    End Sub
    I'm not going to do it for you, so hopefully you will learn the basics:
     
  3. Visual Basic Matt

    Visual Basic Matt Apprentice

    Joined:
    Jan 29, 2008
    Posts:
    647
    Referrals:
    2
    Sythe Gold:
    56
    Discord Unique ID:
    223154494878253056
    How do I make a msg box come up on an error?

    Thanks covey i kno what to do...
     
< Barrows Tracker for Runescape | How to use extension methods in VB 2008 >


 
 
Adblock breaks this site