From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Ending a nested script

Hello,

 

How do I end a script that has been called by another script such that the parent script then continues to run?

 

For example, I have a parent script that undertakes some calculations and then calls another script. I want the child script to check a condition and if that condition is not satisfied then I want the child script to stop and control to pass back to the parent script.

 

A problem is that the scripts I'm working with are pre-existing and long so I can't modify them too much. The child script currently checks for a condition and if it is not satisfied then it uses the function Autoquit. Unfortunately, that stop all scripts running, not just the child script. I would like an equivalent term that just ends the child script and passes back to the parent.

 

Regards,

 

Simon.

0 Kudos
Message 1 of 4
(5,170 Views)
Option Explicit  'Forces the explicit declaration of all the variables in a script.

call dosomething()

sub dosomething()

  ....
 
  if(leavescript) then
    exit SUB
  end if
  ...

End Sub

Just use code like the above. You can leave the sub at any time without raising an error.

0 Kudos
Message 2 of 4
(5,152 Views)

Hello Andreas,

 

Thanks for your response. I need to give you some more detail: the child scripts are seperate and not sub-routines. So, the code looks like:

 

Call ScriptStart("Child_Script")

 

The condition check is in the child script, which should then finish and return to the parent script.

 

Regards.

 

 

0 Kudos
Message 3 of 4
(5,149 Views)

Hi Simon,

 

I assume you are calling this VBScript with the ScriptStart() command, right?  In that case I'd advise adding a Main() subroutine that contains the calls to all your other subroutines.  If any subroutine returns an error condition, you can then exit out of the Main() subroutine with the Exit Sub command Andreas mentioned.  You either need to pass an Errors variable to each of your subroutines or turn them into functions and return the Errors variable (you can call this anything, doesn't have to have that Errors name).

 

Call Main()

Sub Main()
  Dim Errors
  Call FirstSub(Errors)  : IF (Errors) THEN Exit Sub
  Call SecondSub(Errors) : IF (Errors) THEN Exit Sub
  IF FirstFunction()  THEN Exit Sub
  IF SecondFunction() THEN Exit Sub
End Sub ' Main()

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

 

0 Kudos
Message 4 of 4
(5,145 Views)