×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to end a script in the middle?

How to end a script in the middle?

How to end a script in the middle?

(OP)
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

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

trollacious, using a vbYesNoCancel will not exit the script, only bypass the If Then section.

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

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

All we've been shown is a snippet.

vampirebat

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

(OP)
marks,
   thanks a bunch. i'm gonna try that.
:)

RE: How to end a script in the middle?

(OP)
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.

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

(OP)
phv,
  thanks a bunch. that makes perfect sense.

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

Quote:

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 http://www.thespidersparlor.com/vbscript

RE: How to end a script in the middle?

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
www.titangs.com

RE: How to end a script in the middle?

the first computer programming (not strictly, of course) book i read was 'html, a language of style'. i would recommend it

RE: How to end a script in the middle?

(OP)
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?

RE: How to end a script in the middle?

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)

RE: How to end a script in the middle?

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

RE: How to end a script in the middle?

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.

vampirebat

RE: How to end a script in the middle?

yeah, thanks earthandfire, i usually do, what can i say. not sure i agree with your 'just' though smile

RE: How to end a script in the middle?

Ok, but I did warn you that I was being pedantic, though and it was also meant in relation to trollacious's last post where he talked about readability. wink

vampirebat

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close