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

Passing Variables

Status
Not open for further replies.

rbri

Programmer
Jun 27, 2002
84
US
Hello Everyone
I am trying to write a little utility using hta as the GUI for vbscript. I am trying to pass a variable form one function to another function in the same program. here is my code

'-----------------------------------------------------------
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objShell

Set objShell = CreateObject("WScript.shell")

'-----------------------------------------------------------

Function builddir(strChgDir)

Dim strCmd1, strDir

strDir = strChgDir
If strDir = "" Then
strDir = "H:\"
End If
strCmd1 = "cmd.exe /C dir /B /A:D /S " & strDir & " >C:\Temp\tmpdirlist.txt"
objShell.run strCmd1, 1, TRUE

End Function

'-----------------------------------------------------------

Function chngdir()

Dim strChgDir

strChgDir = InputBox("Enter new Drive letter to Browse")
MsgBox "You are going to Browse " & strChgDir

End Function

'-----------------------------------------------------------

'Call builddir() Function to execute
builddir(strChgDir)

'-----------------------------------------------------------

I get an error with this code. The debugger reports that I have an undefined variable and the spot where I call the function to execute is highlighted by the debugger. If I attempt to define this variable in the builddir() function I get a different error message "Name Redefined" and the debugger highlights the Dim line in the builddir() function. Any help someone can give to me will be greatly appreciated.

Thanks Randy

 
I can't tell where this script is sitting in your HTA. It looks like you want to excute
Code:
builddir(strChgDir)
before the page has finished loading though. Is this true?

Also, you don't seem to have a variable called
Code:
strChgDir
to pass to
Code:
builddir()
. All I see is the formal parameter in the definition of
Code:
builddir()
.

So, yeah, the debugger is right. At the point where you perform this function call there isn't anything called
Code:
strChgDir
.

Plus, you declared functions, but then do sub calls to them. For that matter you aren't assigning return values in your functions either.


This is probably the wrong site to come to if you are trying to start out learning VBScript. Have you tried resources like ?
 
Both functions are setting at the beginning of the program
in the header section. The variable strChgDir is initialized in the chngdir() function located just below the builddir() function. Here is the whole program

<HTML>
<HEAD>
<HTA:APPLICATION
ID = &quot;oApp&quot;
APPLICATIONNAME = &quot;HTA Application Tag 2001&quot;
BORDER = &quot;thick&quot;
CAPTION = &quot;yes&quot;
ICON = &quot;H:\rbri_VBScript\ugutil.ico&quot;
SHOWINTASKBAR = &quot;yes&quot;
SINGLEINSTANCE = &quot;yes&quot;
SYSMENU = &quot;yes&quot;
WINDOWSTATE = &quot;normal&quot;
SCROLL = &quot;yes&quot;
SCROLLFLAT = &quot;yes&quot;
VERSION = &quot;1.0&quot;
INNERBORDER = &quot;yes&quot;
SELECTION = &quot;no&quot;
MAXIMIZEBUTTON = &quot;yes&quot;
MINIMIZEBUTTON = &quot;yes&quot;
NAVIGABLE = &quot;yes&quot;
CONTEXTMENU = &quot;yes&quot;
BORDERSTYLE = &quot;normal&quot;
>

<SCRIPT Language=&quot;VBScript&quot;>
<!--
' Author:- Randy Briggin
' Date:- Nov. 21, 2002
' Description:- This program executes UG programs through an hta interface
' Comments:-
Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim strMsg1, objFso, objShell, strUgFlex
Public strChgDir

Set objFso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objShell = CreateObject(&quot;WScript.shell&quot;)
strUgFlex = &quot;UGII_LICENSE_FILE=27000@flfe1ia0.flt.acr.gmeds.com&quot;

'--------------------------------------------------------------------------------

Function builddir()

Dim strCmd1, strDir

strDir = strChgDir
If strDir = &quot;&quot; Then
strDir = &quot;H:\&quot;
End If
strCmd1 = &quot;cmd.exe /C dir /B /A:D /S &quot; & strDir & &quot; > C:\Temp\tmpdirlist.txt&quot;
objShell.run strCmd1, 1, TRUE

End Function

'---------------------------------------------------------------------------------

Function chngdir()

' Dim strChgDir

strChgDir = InputBox(&quot;Enter new Drive letter to Browse&quot;)
MsgBox strChgDir

End Function

'---------------------------------------------------------------------------------


Function chk()

strMsg1 = &quot;Looks Good&quot;
MsgBox strMsg1

End Function

'----------------------------------------------------------------------------------

builddir()


-->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<marquee ALIGN=&quot;Center&quot; LOOP=&quot;infinite&quot; BEHAVIOR=&quot;slide&quot;
BGCOLOR=&quot;#FFFFFF&quot; DIRECTION=&quot;left&quot; HEIGHT=30 WIDTH=100>
<H1><FONT color=#0000FF>Delphi</FONT></H1>
</marquee>
</CENTER>
<H1><CENTER>Unigraphics Utility Menu</CENTER></H1>
<HR>
<BR>
<FORM NAME=ugutil>
<TABLE COLUMNS=&quot;2&quot;>
<TD ALIGN=&quot;left&quot; VALIGN=&quot;top&quot;>
<LEFT>
<FIELDSET STYLE=&quot;font-size:20px; WIDTH:227px;&quot;>
<LEGEND><FONT size=5 color=blue>ACTIONS</FONT></LEGEND><BR>
&nbsp&nbsp&nbsp<SPAN STYLE=&quot;color:green;&quot; ONCLICK=&quot;chk()&quot;>Inspect Partfile Structure</SPAN><BR>
&nbsp&nbsp&nbsp<SPAN STYLE=&quot;color:green;&quot; ONCLICK=&quot;chk()&quot;>Inspect Partfile</SPAN><BR>
&nbsp&nbsp&nbsp<SPAN STYLE=&quot;color:green;&quot; ONCLICK=&quot;chk()&quot;>Convert Partfile</SPAN><BR><BR><BR>
&nbsp&nbsp&nbsp<SPAN STYLE=&quot;color:red;&quot; ONCLICK=&quot;chk()&quot;>Exit</SPAN>
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
</FIELDSET>
</LEFT>
</TD>
<TD ALIGN=&quot;center&quot; VALIGN=&quot;middle&quot;>
<H3>Directory&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspFiles<BR>&nbsp&nbsp
<SELECT NAME=&quot;dirlist&quot; SIZE=11>
<SCRIPT LANUAGE=VBSCRIPT>
Dim strLine, objFso, strFileDirLst, objShell, aFileContests, strVar, strChkVar
Set objFso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set strFileDirLst = objFso_OpenTextFile(&quot;C:\Temp\tmpdirlist.txt&quot;, 1)
aFileContents = Split(strFileDirLst.ReadAll, vbCrLf)
For Each strLine In aFileContents
strVar = &quot;<OPTION VALUE=&quot; & strLine & &quot;>&quot; & strLine & &quot;</OPTION>&quot;
Document.Write strVar
Next
</SCRIPT>
</SELECT>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<SELECT NAME=&quot;filelist&quot; SIZE=11>
<OPTION VALUE=&quot;FILE1&quot;>File one</OPTION>
<OPTION VALUE=&quot;FILE&quot;>File two</OPTION>
<OPTION VALUE=&quot;FILE&quot;>File three</OPTION>
</SELECT><BR><BR><BR><BR>
<INPUT TYPE=&quot;Submit&quot; VALUE=&quot; Change Drive &quot; onclick=chngdir()><BR>
<INPUT TYPE=&quot;Submit&quot; VALUE=&quot;Browse Directory&quot; onclick=chk()><BR>
</TD>
</FORM>
</BODY>
</HTML>

Thanks Randy
 
I'm lost here.

I have:
[ul][li]pasted your code into a blank .hta file,
[li]edited out the ICON line 'cause I didn't have it,
[li]changed the start drive letter to C:\ 'cause I've no H:\ drive here
[li]Change cmd.exe to command.com 'cause I'm testing under Win98SE on this machine[/ul]
Works fine. Well there are still bugs I guess, but I get no script errors and it loads up that listbox with the DIRs from my C:\ drive.

I'm stumped. What's broken?

By the way, why run a CMD.EXE DIR instead of using the FSO to retrieve directory and file names?

 
Hello

The above code also works fine for me because the directory is harded coded in. The problem I have and the errors I receive are when I attempt to change directories. I can't seem to pass the variable from the chngdir() function to the builddir() function. Why I used cmd instead of FSO. I thought it would be easier to create and populate my select box in the body of the html code.

Thanks Randy
 
Ok, I looked at it again, and now I see what I saw before. I guess I assumed this was a bug you already knew about.

You have a blob of inline script in the body of your document that gets executed during page-load. How were you hoping this would get executed a second (third, etc.) time?

Basically, I have doubts that your use of document.write() at load-time is the way to go to build your SELECT list.

Oh! I finally figured out why your page is doing anything at all. &quot;Change Drive&quot; is a SUBMIT button.

You have the page submitting back to itself, causing a page-reload, and thus invoking all of your script all over again. Wondered why the page was blanking out.

Hmm, this sure is a different way to build an HTA.

You could do it this way I guess, but it isn't the usual way I've seen HTAs structured. You're going to lose anything your page's 1st incarnation has &quot;learned&quot; when it dies and enters its 2nd incarnation after clicking one of those SUBMIT buttons.

If you really want to do it this way... well, you're going to have to pass info from one page to another through some mechanism. A text file? Query parameters? Something.

Most HTAs I've seen are built more like a VB program, responding to events and taking actions that way, like repopulating a SELECT list at run-time, not just at load time.

What you have here is the HTA equivalent of a program that takes some input, then triggers another run of itself and dies.

I'm not saying it's wrong (who can say that?) but it isn't an approach I'd consider. Sort of like shelling DIR to examine the directory. Way slow and clunky. If your HTA gets very complex it'll be a nightmare passing state info between &quot;runs&quot; of your &quot;program.&quot;

HTA programming is not transactional like ASP development for example is. An HTA is normally more procedural or even conversational, again more like a VB program than a &quot;web&quot; application. The basic idea is more like a WSH script with a GUI.
 
Hello

I should explain I am new to vbscript and hta I have more experience in UNIX scripting perl posix etc. This is what I am trying to do. I am trying to use hta as a GUI interface only to execute several command line programs. My thought was on load of the hta it would display a list of directories residing in the users home directory including the home directory in the left select window. Plus a listing of all the files in the home directory in the right select window. On the far left is a list of command line programs that the user chooses to use. After the hta program is executed and displayed on the screen the user would then either pick a file from the right window or pick a directory pick the browse directory button then pick a file from the new listing in the right window. After picking the file the user would then pick what command line program they wanted to execute against the file they picked.
In the event that the file they wanted was not under thier home directory structure then they would need to change directories and that is where my problem is. I am not aware of another way to populate the select list and I don't know how to dynamically change the contents of a select list without using a submit button and reload the page. If you can direct me to a book that can help me better learn hta
and vbscript I would greatly appreciate that.

Thanks Randy
 
I'd love to oblige you Randy, but I haven't found a single book with anything but a cursory treatment of HTAs.

My own theory is that hardly anybody writes HTAs.

One issue is the nasty press they've gotten because HTAs can be exploited for certain nasty purposes, though in reality a desktop-resident HTA is no more dangerous than any other Windows application.

Another is that most desktop scripters don't have the DHTML skills to write them, 'cause they aren't &quot;web johnnies.&quot;

And those with the web skills needed just aren't that interested in creating desktop applications. Most HTAs are just that - desktop applications, or web-enabled desktop applications.

So here we are...

Maybe I can help by posting your HTA with some changes to make it do more of what you'd like? I've kept a lot of it intact, and sorta raped the rest. Sorry about that.

If you copy/paste this into a blank HTA file and try it, you'll see what I mean.

Note that I have it defaulting to C:\ for testing instead of the G:\ or whatever you have above. Just change the cosntant I added near the top of the page code.

Also note the event handler at the end of the script block called window_onload(), this gets called as soon as the page finishes loading.

I've also used VBScript-style event-handler linking for the button clicks (no longer SUBMIT buttons) instead of the JavaScript-style handler lashup you were using (onclick=&quot;...&quot; type syntax). This is done by naming the Function or Sub something like &quot;button_onclick().&quot;

Note also that while event handlers CAN be Functions, this is only used to return an event-cancel indication when (rarely) needed. In most cases these oughta be Subs instead as shown here. In VBScript a Function is used to return a result.

Ok, blah, blah, blah... I hope this gives you an idea anyway though. There just isn't a lot published on HTAs, but if you find something be sure to mention it back here.

<HTML>
<HEAD>
<HTA:APPLICATION
ID = &quot;oApp&quot;
APPLICATIONNAME = &quot;HTA Application Tag 2001&quot;
BORDER = &quot;thick&quot;
CAPTION = &quot;yes&quot;
SHOWINTASKBAR = &quot;yes&quot;
SINGLEINSTANCE = &quot;yes&quot;
SYSMENU = &quot;yes&quot;
WINDOWSTATE = &quot;normal&quot;
SCROLL = &quot;yes&quot;
SCROLLFLAT = &quot;yes&quot;
VERSION = &quot;1.0&quot;
INNERBORDER = &quot;yes&quot;
SELECTION = &quot;no&quot;
MAXIMIZEBUTTON = &quot;yes&quot;
MINIMIZEBUTTON = &quot;yes&quot;
NAVIGABLE = &quot;yes&quot;
CONTEXTMENU = &quot;yes&quot;
BORDERSTYLE = &quot;normal&quot;
>

<SCRIPT Language=&quot;VBScript&quot;>
<!--
' Author:- Randy Briggin
' Date:- Nov. 21, 2002
' Description:- This program executes UG programs through an hta interface
' Comments:-
Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const cstrDefaultDir = &quot;C:\&quot;
Dim strMsg1, objFso, objShell, strUgFlex
Dim strChgDir

Set objFso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set objShell = CreateObject(&quot;WScript.shell&quot;)
strUgFlex = &quot;UGII_LICENSE_FILE=27000@flfe1ia0.flt.acr.gmeds.com&quot;

'--------------------------------------------------------------------------------

Sub builddir()
Dim lngOption, folStart, folEach, filEach, objOption

For lngOption = ugutil.selFileList.length - 1 To 0 Step -1
ugutil.selFileList.options.remove(lngOption)
Next

For lngOption = ugutil.selDirList.length - 1 To 0 Step -1
ugutil.selDirList.options.remove(lngOption)
Next

Set folStart = objFSO.GetFolder(strChgDir)
For Each folEach In folStart.SubFolders
Set objOption = document.createElement(&quot;option&quot;)
objOption.value = strChgDir & folEach.Name & &quot;\&quot;
objOption.text = strChgDir & folEach.Name & &quot;\&quot;
ugutil.selDirList.options.add objOption
Next

For Each filEach In folStart.Files
Set objOption = document.createElement(&quot;option&quot;)
objOption.value = filEach.Name
objOption.text = filEach.Name
ugutil.selFileList.options.add objOption
Next

Set objOption = Nothing
Set filEach = Nothing
Set folEach = Nothing
Set folStart = Nothing
End Sub

'--------------------------------------------------------------------------------

Sub btnBrowseDir_onclick()
strChgDir = ugutil.selDirList.value
builddir
End Sub

'---------------------------------------------------------------------------------

Sub btnChgDrive_onclick()
strChgDir = InputBox(&quot;Enter new Drive letter to Browse&quot;)
If strChgDir = &quot;&quot; Then
strChgDir = cstrDefaultDir
Else
strChgDir = Left(strChgDir, 1) & &quot;:\&quot;
End If
builddir
End Sub

'---------------------------------------------------------------------------------

Function chk()

strMsg1 = &quot;Looks Good&quot;
MsgBox strMsg1

End Function

'----------------------------------------------------------------------------------

Sub window_onload()
strChgDir = cstrDefaultDir
builddir
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<marquee ALIGN=&quot;Center&quot; LOOP=&quot;infinite&quot; BEHAVIOR=&quot;slide&quot;
BGCOLOR=&quot;#FFFFFF&quot; DIRECTION=&quot;left&quot; HEIGHT=30 WIDTH=100>
<H1><FONT color=#0000FF>Delphi</FONT></H1>
</marquee>
</CENTER>
<H1><CENTER>Unigraphics Utility Menu</CENTER></H1>
<HR>
<BR>
<FORM NAME=ugutil>
<TABLE COLUMNS=&quot;2&quot;>
<TD ALIGN=&quot;left&quot; VALIGN=&quot;top&quot;>
<LEFT>
<FIELDSET STYLE=&quot;font-size:20px; WIDTH:227px;&quot;>
<LEGEND><FONT size=5 color=blue>ACTIONS</FONT></LEGEND><BR>
<SPAN STYLE=&quot;color:green;&quot; ONCLICK=&quot;chk()&quot;>Inspect Partfile Structure</SPAN><BR>
<SPAN STYLE=&quot;color:green;&quot; ONCLICK=&quot;chk()&quot;>Inspect Partfile</SPAN><BR>
<SPAN STYLE=&quot;color:green;&quot; ONCLICK=&quot;chk()&quot;>Convert Partfile</SPAN><BR><BR><BR>
<SPAN STYLE=&quot;color:red;&quot; ONCLICK=&quot;chk()&quot;>Exit</SPAN>
<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>
</FIELDSET>
</LEFT>
</TD>
<TD ALIGN=&quot;center&quot; VALIGN=&quot;middle&quot;>
<H3>Directory Files<BR>
<SELECT id=&quot;selDirList&quot; SIZE=11>
</SELECT>
<SELECT id=&quot;selFileList&quot; SIZE=11>
</SELECT><BR><BR><BR><BR>
<INPUT type=&quot;button&quot; id=&quot;btnChgDrive&quot; value=&quot; Change Drive &quot;><BR>
<INPUT type=&quot;button&quot; id=&quot;btnBrowseDir&quot; value=&quot;Browse Directory&quot;><BR>
</TD>
</FORM>
</BODY>
</HTML>

Good luck!
 
Hello

WOW I've got alot to learn! I would have never thought to code those select lists that way but thats exactly what I was trying to accomplish. Thank you very much for your time and help! That helped and showed me alot.

Thanks Randy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top