Getting Creative with HTAs
Chris shows how you can take your HTAs one step further.
- By Chris Brooke
Over the past two months [see "Going
," 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
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.
And replace it with this:
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.
Set objFSO = _
Set objFile = _
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
Set objOption = _
objOption.Text = strLine
objOption.value = strLine
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.
("Enter the computer’s IP address", "192.168.1.1")
If IsNull(strIPAddr) Then Exit Sub
If strIPAddr=Empty Then Exit Sub
"Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source="
"SELECT * FROM " & DBTable.InnerHTML , _
objConn, adOpenStatic, adLockOptimistic
strHTML = "<table border='1' " & _
"style='border-collapse: collapse' " & _
"bgcolor='white' bordercolor='black' " & _
"width='50%' id='Table1' >"
Do Until objRS.EOF
strHTML = strHTML & "<tr>
strHTML = strHTML & "<td width='500'>" & _
objRS.Fields.Item("Computername") & "</td>"
strHTML = strHTML & "<td width='300'>" & _
objRS.Fields.Item("IPAddress") & "</td>"
strHTML = strHTML & "</tr>"
strHTML = strHTML & "</table>
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.
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
to download the entire script in a .txt document.
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].