Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

I need some help in Visual Basic (Try/Catch Blocks)?

I am making a project where the user puts their name and sales amount in two different text boxes, and it gives the amount of other things such as commission, deductions, net pay, etc.

If the user does not put a name into either of the first two text boxes, an error message must come up.

I got the sales textbox error message to work:

Catch ex As Exception

MessageBox.Show("Sales amount must be numeric", "Invalid Input", MessageBoxButtons.OK)

But when I try the same thing for the name textbox, it does not work. I am not sure what to put in the Try area of the code.

For instance:

Try

nameTextBox.Text = ""

Catch ex As Exception

MessageBox.Show("Enter a name", "Invalid Input", MessageBoxButtons.OK)

I probably am not making myself clear enough to answer this, but I just don't understand what to put under the "Try" area of my script in order for it to catch the error.

Any help is appreciated

Update:

should say "if user does not put name into first text box or sales amount into 2nd text box, it should give error messages"

Update 2:

ohh, I am a complete novice at this, and didnt think to do something that wasn't try/catch because it wasnt taught to me lol.

Was much easier than I made it out to be, thanks.

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    I wouldn't use Try...Catch in that instance... is there an "OK" button, or some button that processes this stuff after the user enters the data?

    In that button's code, just have a couple IF statements, one for each check that you do. The statement itself will have the check, if it fails, go into the IF statement ~ call up a msgbox telling the user how they screwed up, and then do a 'return' to exit the button's processing.

    After the checks at the beginning of the button's code, then you do your processing of the data.

Still have questions? Get your answers by asking now.