Can I pass value from VB form to VBS?

D

D.P. Roberts

I have 3 vbscripts and a vb form with radio buttons corresponding to each
script. The form's only purpose is to provide a nice GUI for the user to
decide which of the 3 scripts to run. Now, because the scripts are mostly
identical to one another, I'd really like to combine them into one script.
But I can't for the life of me figure out how to pass a radio button value
from a vb form to the script so the appropriate portion of the script is
executed based on which radio button was selected. Basically I need the
script to do something like this:

RBselection = <the radio button selected on the vb form>

Select Case RBselection
Case RadioButton1
varSelection = 1
Case RadioButton2
varSelection = 2
Case RadioButton3
varSelection = 3

If I could somehow do this, then I could use the varSelection value to run
the necessary parts of the script. Anyone know if this is possible?

Thanks in advance...
 
D

Dmitriy Antonov

As I understand you are talking about WScript.
There is collection of command-line arguments in WScript object (which is
global object there and you don't need to declare it)

Then from your VB application you can call vbs like:

'this is code in VB to call vbs
Dim sComStr As String
'vbs file is just an argument of script engine (wscript.exe)
'you might need to "play" with paths to files
sComStr = "wscript.exe " & "test.vbs " & RBselection 'add argument
according to your radio-button selection
ChDir App.Path 'set current folder to that, where vbs file and
wscript.exe are located (I put them together into vb's project folder for
simplicity)
Shell sComStr 'note Shell command doesn't like paths with spaces - there
are ways around

For testing I've created the following small script (in my case it is
test.vbs)

dim varTest
If WScript.Arguments.Count then varTest=WScript.Arguments(0)
msgbox "Argument " & varTest


As a result, when I call this script with some value of RBselection, then
msgbox with this value is shown

Hope, this is what you need. If you still need to read VB's data from within
vbs, then your VB must be built as ActiveX component and then you can expose
its public interface to vbs via late binding.

Dmitriy,
MCSD.
 
J

Jeff Johnson [MVP: VB]

I have 3 vbscripts and a vb form with radio buttons corresponding to each
script.

Is this a VB.NET question or a "classic" VB (6 or earlier) question? Because
you have crossposted between .NET (*.dotnet.*) and non-.NET (*.vb.*) groups.
Pick one.
 
A

Andrew D. Newbould

D.P. Roberts said:
I have 3 vbscripts and a vb form with radio buttons corresponding to each
script. The form's only purpose is to provide a nice GUI for the user to
decide which of the 3 scripts to run. Now, because the scripts are mostly
identical to one another, I'd really like to combine them into one script.
But I can't for the life of me figure out how to pass a radio button value
from a vb form to the script so the appropriate portion of the script is
executed based on which radio button was selected. Basically I need the
script to do something like this:

RBselection = <the radio button selected on the vb form>

Select Case RBselection
Case RadioButton1
varSelection = 1
Case RadioButton2
varSelection = 2
Case RadioButton3
varSelection = 3

If I could somehow do this, then I could use the varSelection value to run
the necessary parts of the script. Anyone know if this is possible?

Thanks in advance...

Rather than Shell out to execute the vbs scripts why not add the
ScriptControl to you VB6 program.

You can then load the script into it and use the AddObject method to
pass your variables into it. The safer way would be to define a small
dummy class containing the data you wish to pass, dim a local object to
that class and then use AddObject to pass the local object.

You then only have to put your existing code into a sub or function so
you can call it from VB. Worst case you could combine all three scripts
into one using three functions and use VB to call the one required.
Easy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top