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: 

Sub procedure

Solved!
Go to solution
Is it possible to put a sub precedure inside another sub procedure? I try to 
do it but it does not work. Any ideas please?
0 Kudos
Message 1 of 8
(3,380 Views)

Hi Amanda,

It is not possible to define a procedure inside another procedure:

sub Procedure1()
	'do something
	sub Procedure2()
		'do something else
	end sub
end sub

But you can call a procedure inside another procedure:

sub Procedure1()
	'do something
	call Procedure2()
end sub

sub Procedure2()
	'do something else
end sub
Christian
CLA, CTA, CLED
0 Kudos
Message 2 of 8
(3,347 Views)
In fact, I created an easy dialog that contains two buttons, and I activated the 
eventclick for each button, and so inside '' sub Button1_eventclick ...) i have
to Introduce my script. Besides, my script already contains several SUB
procedures. How can I do? could I replace the SUB
procedure with others that have the same utility? another
solutions? Thank you for your help
 
0 Kudos
Message 3 of 8
(3,315 Views)

Hi,

I'm not sure if I understand your question correctly.

I suppose you want to define your procedures in an external file and call them in a SUD dialog?

If this is the case you could do it like this:

Dialog script:

Sub Dialog_EventInitialize(ByRef This) 'Erzeugter Event-Handler
	call ScriptInclude(CurrentScriptPath & "Procedures.vbs")
End Sub

Sub Button1_EventClick(ByRef This) 'Erzeugter Event-Handler
	call OpenMsgBox()
End Sub

Procedures.vbs (saved in the same folder like the sud file):

sub OpenMsgBox()
	call MsgBox("Hello World!")
end sub

If this does not help, can you please provide more detailed information about what you want to do?

Regards

Christian
CLA, CTA, CLED
0 Kudos
Message 4 of 8
(3,285 Views)

With ScriptInclude you can include different versions of your script.

If there is another command with the same name that was already included before, it will be overwritten.

If you have two buttons that have to execute two different versions of the same procedure you can do it like this:

Sub Button1_EventClick(ByRef This) 'Erzeugter Event-Handler
	call ScriptInclude(CurrentScriptPath & "Procedures1.vbs")
	call OpenMsgBox()
End Sub

Sub Button2_EventClick(ByRef This) 'Erzeugter Event-Handler
	call ScriptInclude(CurrentScriptPath & "Procedures2.vbs")
	call OpenMsgBox()
End Sub

Where Procedures1.vbs and Procedures2.vbs both include a procedure with name OpenMsgBox but different content.

But DIAdem help does not recommend to use ScriptInclude more than once:

http://zone.ni.com/reference/en-XX/help/370858M-01/comoff/scriptinclude/

Christian
CLA, CTA, CLED
0 Kudos
Message 5 of 8
(3,276 Views)
Thank you very much for your help, But i am still blocked and I do not 
know how to proceed. Indeed, I will try to explain more the
problem:
I have three totally different scripts, which perform three different functions.
And I want to create a dialog box that contains three buttons each linked to a
script. The problem is that inside each script I have several Sub procedure,
and when I want to assemble it with a button, I find a problem with the sub
procedure, like it is shown in the picture attached. do you have any idea
please??

 

0 Kudos
Message 6 of 8
(3,246 Views)
Solution
Accepted by topic author amanda78

Hi,

It looks like you are using the recording mode to create simple code snippets.

Obviously one of these snippets with the InitMyFolders procedure was copied to another script, right?

Please have a look at my first post again. It shows in the first example that it's not possible to create a procedure inside another procedure, which is exactly what you are trying to do in your example.

You can either just delete the procedure and define the array variable directly inside the other procedure like this:

Sub Button4_EventClick(ByRef This) 'Erzeugter Event-Handler
  '...
  dim MyFolders()
  redim MyFolders(2)
  MyFolders(0) = "P:\results\"
  MyFolders(1) = "X:\results\"
  '...
End Sub

or you can move the InitMyFolders procedure outside the other procedure like this (which is the same like in the second example of my first post):

sub InitMyFolders
  redim MyFolders(2)
  MyFolders(0) = "P:\results\"
  MyFolders(1) = "X:\results\"
end sub

Sub Button4_EventClick(ByRef This) 'Erzeugter Event-Handler
  '...
  dim MyFolders()
  call InitMyFolders()
  '...
End Sub

I hope this helps. Also have a look at some VBScript tutorials about functions and procedures like here.

Regards

Christian
CLA, CTA, CLED
0 Kudos
Message 7 of 8
(3,202 Views)

thank you so much !! it works . 

Regards

0 Kudos
Message 8 of 8
(3,185 Views)