Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stop the code from running

Status
Not open for further replies.

sf123

Programmer
Oct 9, 2002
30
US
I am validating user input in my form. I am coding msgbox if any text box is blank. I am also checking for integers etc. Every time the msgbox line executes and user clicks ok, I want them to view the form and be able to enter a new input. My problem is that my program continues running.
My code is:
If (txtFrom.Text = "" Or txtTo.Text = "") Then
MsgBox ("You didn't enter a valid input")

Else
If (IsNumeric(txtSynthFrom.Text) And IsNumeric(txtSynthTo.Text)) Then
specifiedFrom = txtFrom.Text
specifiedTo = txtTo.Text
Else
MsgBox ("You didn't enter a number")
End If
End If
I thought this was pretty simple code. Am I missing something?
 

Use Exit Sub or Exit Function (depending upon which you code is place in) on the line after the msgbox to exit your validation routine.
 
I called 5 subs from my submit_click. If I do exit sub, it will just continue with the rest of the code which is calling the next sub. I want all my code to halt (but not end because I still want my form to be running), bring the user back to the form, let them resubmit and have code start from begining again. I've done msgboxes before and I thought this was the default. I don't recall having to code this logic.
 
Use the Validate Event of the textboxes to check for valid data, then the focus will be kept on the textbox if invalid data is entered.

Rob
 

Have you prevously coded all of your validation routines in one sub? Sounds like it, but then again I have been wrong before.

The easiest thing to do is change your subs into functions that return boolean values. When you call them test the boolean value that is returned (Either True or False however you want to code it). Place the value that you want to return where the msgbox is for failure.

Note: If you do use this suggestion then let me tell you that a boolean function will return false by default.

So you would do something like...

[tt]
Private Sub Command1_Click()

If Validation_Event1 = True Then Exit Sub
If Validation_Event2 = True Then Exit Sub
If Validation_Event3 = True Then Exit Sub
If Validation_Event4 = True Then Exit Sub
If Validation_Event5 = True Then Exit Sub

Call UpdateRoutine

End Sub

[/tt]

I hope this helps. Good Luck

 
rdavis:
There is a validate event in Textbox? I tried the name of my textbox. but nothing of that sort poped up. What do you mean?
 
When you double click the textbox to see the code, the default event is 'Change'. Change this to 'Validate' and you will see the subroutine for this event.

Rob
 
I know I am in the wrong forum for this but it has proven to be very helpful. My problem is that I am using visual basic for application in excel so I don't have the validate. (I checked in vb5&6 and saw that there is validate over there). If you have any other suggestions of how I can do this please let me know.
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top