HTA TutorialHTA Tutorial #12

The final piece of the jigsaw puzzle you are missing is the way I have used the Dictionary object. This is provided by VBScript and it seems a natural way to do a database. Normally, you would use arrays but this seemed to offer a purpose built way to handle data and it seems to work very well. Here's some test code:

<html>
<head>
<HTA:APPLICATION>
<script language="VBScript">
Dim a, d, i, s
  
	Set d = CreateObject("Scripting.Dictionary")
  	d.Add "a", "Athens" 
  	d.Add "b", "Belgrade"
  	d.Add "c", "Cairo"

  	a = d.Items
  	For i = 0 To d.Count -1
    		s = s & a(i) & vbcrlf
  	Next
  	msgbox s
</script>
</head>
</html>

This will put data into the dictionary and then take it out again and display it. The dictionary is transitory (is empty when the program is shut down and restarted) so it has to be filled with data each time the program is started usually from a file.

<html>
<head>
<HTA:APPLICATION>
<script language="VBScript">
Dim a, d, i, s

The file starts as usual and some variables are created.

Set d = CreateObject("Scripting.Dictionary")

We create an object which is the scripting dictionary.

d.Add "a", "Athens"
d.Add "b", "Belgrade"
d.Add "c", "Cairo"


And then add as many items as we like. The form of this is the object name then the command to add followed by a key (this could be numeric) followed by a comma and then the data.

a = d.Items

Now we want to read back what was put into the dictionary.

For i = 0 To d.Count -1

Start a loop starting from zero to the number of items in the dictionary (the array starts at zero in the dictionary so we subtract one from the count).

s = s & a(i) & vbcrlf

Now we construct a string using the data extracted with the loop.

Next
msgbox s
</script>
</head>
</html>

We then display the data and finish and that's it. To give you a feel for how programs are constructed, here is an example of how a simple user interface can be built to write data to a file (more work needs to be done to read it back again).

<HTML>
<BODY>
<HEAD>
<title>Save</title>
<HTA:APPLICATION ID="Save" 
    APPLICATIONNAME="Save" 
    BORDER="thick"
    CAPTION="yes"
    ICON="save.ico"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    WINDOWSTATE="normal"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="yes">

<Script Language=JScript>
window.resizeTo(200,150);
window.caption="Save";
</SCRIPT>

<Script Language=VBS>
Sub subSave()
	Dim strName
	dim strNumber
	dim fs
	dim a
	dim c

	Const ForAppending = 8

	strName = txtName.value
	strNumber = txtNumber.value

	Set fs = CreateObject("Scripting.FileSystemObject")

	If (fs.FileExists("test.txt")) Then
		' nothing to do
  	Else
		Set a = fs.CreateTextFile("test.txt", True)
		'a.WriteLine("Hello")
		a.Close
  	End If

	Set a = fs.OpenTextFile("test.txt", ForAppending)
	if strName <> "" then
		a.WriteLine(strName)
		a.WriteLine(strNumber)
		txtName.value = ""
		txtNumber.value = ""
	end if
	a.Close
End Sub
</SCRIPT>
</HEAD>
<body scroll="no">
<INPUT TYPE="Text" ID="txtName" size="22"><BR>
<INPUT TYPE="Text" ID="txtNumber" size="22"><P>
<INPUT TYPE="Submit" ID="btnQuit" VALUE="Quit App" onclick=self.close()>
<INPUT TYPE="Submit" ID="Commit" VALUE="Commit it" onclick=subSave()
</BODY>
</HTML>

We are now at the stage when you can take a look at the phone.hta that I have supplied. This could be used as the basis of a much larger application but I'll leave that to you. Good luck.

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please email
nickjc@nickjc.co.uk if you need help