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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to end a script in the middle?

Status
Not open for further replies.

barny2006

MIS
Mar 30, 2006
521
US
hi,
i have a script like this:
Code:
yes = "6"
no = "7"
cancel = "2"
answer = msgbox ("continue?", vbyesnocancel + icon, title)
if answer = no then  
... i like to end the script and quit

... the rest of the code will execute if answer is yes
 
You can always use something like
Code:
yes = "6"
no = "7"
cancel = "2"
answer = MsgBox ("continue?", VBYesNoCancel + icon, title)
If answer = yes Then  

  ... the rest of the code will execute if answer is yes
End If

You offer 3 choices to the user: Yes, No, and Cancel. Your example only indicates that you handle Yes and No.

Lee

 
I understand that. My comment was about the original code where the comments in the code stated that if the answer was No, the script should terminate, but if it was Yes, it should execute the rest of the script. Even though Cancel was an option with the MsgBox, the code seemed to indicate it would be processed the same as a Yes choice.

The example I provided handled a Yes answer specifically rather than handling a No answer specifically. A No answer would skip the conditional and the script would terminate at the end no matter what the answer.

I know that the practice of one entrance/one exit in a program or sub/function isn't always the first way I see something, in the long run it's usually (though not always, I admit) more easily modified and maintained to program that way. That's what I was aiming for with that example.

Lee
 
... but what if there was more code after the End If?

All we've been shown is a snippet.

[vampire][bat]
 
Hi, earthandfire.

Anything to be run for a non-No answer would be inside the conditional, and if there was anything common to Yes and Cancel answers there would have to be a conditional outside the one in my example. Since the question was how to terminate the script with a No answer, nothing would be outside the conditional(s) before the end of the script.

Lee
 
troll,
i would use the if ... then... stuff. but the code in the middle is pages long with several loops and stuff. so it would be confusing.
thanks a bunch.
 
Why not simply this ?
yes = "6"
no = "7"
cancel = "2"
If MsgBox ("continue?", vbYesNo + icon, title) <> vbYes Then
WScript.Quit
End If
'... the rest of the code will execute if answer is yes


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
as anal is it might sound i think Wscript.Quit commands anywhere else other than the logical end of the script (to allow for a return code) is bad form
 
My comment about one entrance and one exit for a program, sub, or function is along the lines of what mrmovie said.

If you have so much code "pages long with several loops and stuff" you should consider breaking things down into separate functions and subs, separating the tasks into logical units. You could call those inside the If statement, which would shorten that significantly. It'd also make things easier to maintain in the future if the tasks were assigned to individual subs and functions that were named something similar to whatever they did.

Wscript.Quit (as well as Exit Sub, Exit Function, Exit For, Exit Do, etc) is a completely legitimate way to terminate a script, but it does violate the popular, common teaching that blocks of code should have one entry and one exit, which I referred to before.

Lee
 
i think Wscript.Quit commands anywhere else other than the logical end of the script (to allow for a return code) is bad form

I'll have to respectfully disagree with you. There are plenty of instances where it makes sense to use Wscript.Quit. When using login scripts that make configuration changes to PCs it is often a GOOD idea to use this. Have your script set some form of registry flag or create some file for checking to determine if the script has already been run on that PC. At the beginning of the script do your check and quit the script if it has already been run and if not take the necessary actions and finish with setting your flag.

This is particularly useful in medium sized businesses or larger organizations that may not have a more robust method for management such as SMS or Tivoli.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
PHV and Markdmac have both been doing this quite awhile and I have always found their recomendations useful.

Here is a quick code snippet example for you that may help out.

Code:
Function InputUserID()
strfuser = InputBox("Enter user ID. Example: 900jsmith" , "Enter Username")
			If strfuser = False Then Call Quit(1)
			If strfuser = "" Then Call Quit(2)
End Function     


Sub Quit(Error1)
	If Error1 = "1" Then MsgBox("Canceled")
	If Error1 = "2" Then MsgBox("Enter and Valid User ID.")
	WScript.Quit
End Sub

Thanks

John Fuhrman
Titan Global Services
 
the first computer programming (not strictly, of course) book i read was 'html, a language of style'. i would recommend it
 
here's the issue:
in the beginning of the script, i have to prompt the user and display the process. then i ask, "do you want to continue?" if they answer "yes" the code is executed and if not, the script terminates. i think wscript would work fine. i haven't tried exit sub. would it work the same way?
 
Dim intError
On Error Resume Next
If InputBox = 1 Then
Call doStuff()
Else
intError = 1
End If
If intError <> 0 Then
Wscript.Quit intError
Else
Wscript.Quit Err.Number
End If

Sub doStuff()
Dim....
Set FSO = ...
Set objTS = FSO.CreateTextFile

objTS.Close
Set objTS = Nothing
Set FSO = Nothing
End Sub


issues with exiting subs and the main body of the script is that you dont get a chance to clear up your objects, i.e. .set them to nothing, or close text streams etc plus if you do tidy up a little before you bomb out then you end up with code relating to objects at different levels of nesting which is just plain ugly, anyway, its just a question of style and whitebox thinking, blackbox providing the end result is the same who cares?? (personally i do)
 
I can tell you from experience that reading someone else's code who ended things where ever and whenever is far more difficult than reading code where there's one entry and one exit to the program, sub, function, loop, or anything else.

That also applies to going over some of my own code after not seeing it for a year or so.

Lee
 
To be pedantic, mrmovie, you are relying on intError being initialised to 0 by VBS - which I consider to be just as poor a programming style. At least for clarity all variables should be initialised by the programmer before use.


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top