Mr. Script

Getting Creative with HTAs

Chris shows how you can take your HTAs one step further.

Over the past two months [see "Going Hyperactive," Jan. 2005, and "An HTA Reality Check," Feb. 2005], I’ve given you a crash course on the world of HTML applications (HTAs). We’ve covered most of the basics, and I hope you’ve gained a foundation of knowledge upon which you can build. Before we move on to other topics, let’s look at some additional areas where HTAs really shine.

Because HTAs are HTML, they’re excellent at displaying data in a variety of formats. You can display text in numerous fonts and with many styles, such as bold, italics, superscript and so on. You can also display data in a formatted table. Because our HTA example from last month dealt with adding data to an Access database, it seems fitting to demonstrate how to display that data properly within an HTA. At the same time, you’re going to learn how to dynamically populate HTA objects.

Our script from last month created a database of computer names and IP addresses. These were originally hard-wired into the script code. As promised, I’ve added the ability to enter records into the database. We’ll start by using the FileSystemObject (objFSO) to collect the names of valid computers from a text file. Once a valid computer is selected, it will prompt the user to enter the IP address. While this may be a bit of a stretch in terms of how we would actually accomplish this particular scripting task in real life, it does have the advantage of demonstrating the technique for dynamically populating a dropdown box.

Let’s Get Going
Because we’re now manually populating the database, the first change is to remove the AddData button and replace it with a DropDown list object.

Remove this:

<input id=AddDataButton
class="button"
type="button"
value="Add Data"
onClick="AddData"
>

And replace it with this:

<select <br>
size="1" <br>
name="Computers"
onChange="AddData">

Because we’ve removed an object, we need to make sure we go through all the Subs and change any reference to the AddDataButton to point to Computers.

The next thing we need to change is the Sub Window_Onload. We’ve entered the names of valid computers in a text file called computers.txt and saved it to the root of C:\. Now, when the script is run, the Window_Onload Sub—in addition to resizing the window and disabling certain objects—retrieves the list from this file and populates a DropDown list for us.

Sub Window_Onload
self.Focus()
self.ResizeTo 640,480
MakeTableButton.Disabled=true
Set objFSO = _
CreateObject("Scripting.FileSystemObject")
Set objFile = _
objFSO.OpenTextFile("c:\computers.txt", 1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set objOption = _
Document.createElement("OPTION")
objOption.Text = strLine
objOption.value = strLine
Computers.add(objOption)
Loop
objFile.Close
Set objFIle=Nothing
Set objFSO=Nothing
Computers.Disabled=true
End Sub

Finally, we need to change the Sub AddData to let users specify an IP address for the computer selected from the DropDown list. And yes, it will allow for more than one IP entry per computer just like NT does.

Sub AddData
strComputerName=Computers.Value
strIPAddr=window.prompt _
("Enter the computer’s IP address", "192.168.1.1")
If IsNull(strIPAddr) Then Exit Sub
If strIPAddr=Empty Then Exit Sub

Const adOpenStatic=3
Const adLockOptimistic=3
Set objConn=CreateObject("ADODB.Connection")
Set objRS=CreateObject("ADODB.Recordset")
objConn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source="
& DBActive.InnerHTML
objRS.Open _
"SELECT * FROM " & DBTable.InnerHTML , _
objConn, adOpenStatic, adLockOptimistic

objRS.AddNew
objRS("Computername")=strComputerName
objRS("IPAddress")=strIPAddr
objRS("Created")=Now
objRS.Update
objRS.MoveFirst

strHTML = "<table border='1' " & _
"style='border-collapse: collapse' " & _
"bgcolor='white' bordercolor='black' " & _
"width='50%' id='Table1' >"
Do Until objRS.EOF
strHTML = strHTML & "<tr> </tr> <tr>"
strHTML = strHTML & "<td width='500'>" & _
objRS.Fields.Item("Computername") & "</td>"
strHTML = strHTML & "<td width='300'>" & _
objRS.Fields.Item("IPAddress") & "</td>"
strHTML = strHTML & "</tr>"
objRS.MoveNext
Loop
strHTML = strHTML & "</table> "

objRS.Close
objConn.Close
Set objRS=Nothing
Set objConn=Nothing
DBData.InnerHTML=strHTML
End Sub

Every time we add a pair of computer and IP addresses, the record set is refreshed and the data is reloaded into the table. Therefore, the display is constantly updated with current information. This involves recreating the table each time. Again, in the real world, this is not the most suitable solution. As the database grows in size, performance decreases. We do it here simply to demonstrate the technique of dynamically creating a table.

Extracurricular Activity
Other interesting functions available to HTAs include accessing the Print dialog, entering masked passwords, changing the cursor and more. You can even have an HTA reload itself, thereby resetting all default values. I encourage you to spend some time experimenting with HTAs, and see how many useful gems you can find.

More Information

Click here to download the entire script in a .txt document.

About the Author

Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].

Featured

comments powered by Disqus

Subscribe on YouTube

Upcoming Training Events

0 AM
TechMentor @ Microsoft HQ
August 11-15, 2025