<html>
<script>
var gnWinCaption= "HTA tester";
var gnWinWide= 400;
var gnWinHigh= 300;
function DoStartup() { // called from BODY ONLOAD
SetWindowSize( gnWinWide, gnWinHigh ); // app init stuff
document.title=gnWinCaption;
}
function SetWindowSize(w,h) { // known bug in mshtml; try/catch fixes
try { window.resizeTo( w, h );}
catch(e){ SetWindowSize(w,h); } // do it until it works
}
//---------------------------------------------------
function DoTest()
{
// instantiate and exercise ActiveX objects, etc.
oDivDisplayArea.innerHTML="<b>test complete!</b>";
}
</script>
<!-- I like to put the U/I stuff at the bottom -->
<!-- ***************************************** -->
<body onLoad='DoStartup();'>
<DIV align=right><input type=button value="restart"
onclick= "document.location.reload();"></DIV>
<input type=button value='DoTest' onclick='DoTest();' </input>
<DIV ID=oDivDisplayArea>
<font color=gray>(stuff will be displayed here)</font>
</DIV>
</body>
</html>
var goDb;
var gsConnect= "DSN=Northwind;UID=;PWD=";
var adOpenStatic= 3; // constants used in oRs.Open(...)
var adLockReadOnly= 1;
var adCmdText= 1;
//---------------------------------------------------
function DoTest()
{
var sSQL="SELECT * FROM Customers ORDER BY CustomerID";
//--------------------------------------- open the db connection
goDb= new ActiveXObject( "ADODB.Connection" );
goDb.Open( gsConnect );
//---------------------------------------- open the recordset
var oRs= new ActiveXObject( "ADODB.Recordset" );
oRs.Open( sSQL, goDb, adOpenStatic, adLockReadOnly, adCmdText );
var sOutput="<TABLE border=3>";
while (! oRs.EOF ) {
var sOneLine= "<TR>";
sOneLine += "<TD>"+ oRs.Fields('CustomerID' ).Value + "</TD>";
sOneLine += "<TD>"+ oRs.Fields('CompanyName').Value + "</TD>";
sOneLine += "<TD>"+ oRs.Fields('City' ).Value + "</TD>";
sOneLine += "</TR>";
sOutput += sOneLine;
oRs.MoveNext();
}
sOutput += "</TABLE>";
oDivDisplayArea.innerHTML= sOutput; // show the output
}
That JScript code creates a couple of ADO objects. It accesses the SQL Server Northwind sample database and opens a recordset on its Customers table. It then cycles through the table and generates some HTML with some of the field data.
Enter Key: <input type=text id='edKey'</input>
var sKey= document.all.edKey.value; // get user input
var sSQL="SELECT * FROM Customers"
+" WHERE CompanyName LIKE '" +sKey+ "%' "
+" ORDER BY CustomerID";
Now the user can input a letter or two in the input box and the resulting output will contain only records in which the CompanyName begins with that (or those) letters.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (2)
Commented:
What is the problem with VB or perl? The Microsoft HTA tutorial pages use VBScript code.
Author
Commented: