HOWTO: How To Create a Simple Query in an ActiveX Layout

ID: Q158737


The information in this article applies to:


SUMMARY

This article provides information on how to create a simple query form using Active Server Pages and ActiveX Layout (ALX) files. This example displays a record from a database in the ALX, and then allows the user to query for a different record, which appears in the same form.


MORE INFORMATION

Below are two sections of code: a main page (Page1.asp) and the ActiveX Layout file (Query.asp.) Save these files to the same directory with the file names as given. Make sure that the directory and the files are saved and has Execute and Read permissions from the Internet Information Server (IIS) Manager.

Main Page: Page1.asp


   <% Response.Expires = 0 %>
   <html>
   <head><title>Title</title></head>

   <body>
   <%
   If Not IsObject(Session("cn")) Then
     REM Create connection object if necessary
     Set cn = Server.CreateObject("ADODB.Connection")
     cn.Open "AdvWorks"

     REM Gather initial data
     Set rs = cn.Execute("SELECT * FROM CUSTOMERS WHERE CustomerID=1")
     Set Session("cn") = cn

     REM Place the information to be displayed in the ALX in session
     REM variables, because the ALX will be parsed AFTER this page loads.
     Session("ID") = rs(0)
     Session("Name") = rs(1)
   Else
     On Error Resume Next

     REM Restore existing connection
     Set cn = Session("cn")

     REM Query is based on ID submitted in form
     MySQL = "SELECT * FROM CUSTOMERS WHERE CustomerID=" &    Request.Form("ID")
     Set rs = cn.Execute(MySQL)
     If Err = 0 Then
     Session("ID") = rs(0)
     Session("Name") = rs(1)
   Else
     REM Query Failed
     Session("ID") = "None Found"
     Session("Name") = "None Found"
     End If
   End If
   %>
   <form name="HiddenForm" action="page1.asp" method="Post">
     <input name="ID" type="Hidden" maxlength="5" size="5" value="Nothing">
   </form>
   <script language="VBScript">
   <!--
   Sub SubmitForm
     Document.HiddenForm.ID.Value = Window.Html_Layout1.IDTextBox.Value
     HiddenForm.Submit
   End Sub
   -->
   </script><object
   classid="CLSID:812AE312-8B8E-11CF-93C8-00AA00C08FDF"
   id="Html_Layout1" style="LEFT:0;TOP:0"><param name="ALXPATH" ref
   value="query.asp"></object>
   </body>
   </html>
  

Layout File: Query.asp


   <SCRIPT LANGUAGE="VBScript">
   <!--
   Sub CommandButton1_Click()
      Window.Parent.SubmitForm
   End Sub
   -->
   </SCRIPT>

   <DIV ID="Layout1" STYLE="LAYOUT:FIXED;WIDTH:225pt;HEIGHT:90pt;">
       <OBJECT ID="IDLabel"
        CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0"
   STYLE="TOP:8pt;LEFT:8pt;WIDTH:99pt;HEIGHT:17pt;ZINDEX:0;">
           <PARAM NAME="Caption" VALUE="Customer ID">
           <PARAM NAME="Size" VALUE="3493;600">
           <PARAM NAME="FontCharSet" VALUE="0">
           <PARAM NAME="FontPitchAndFamily" VALUE="2">
       </OBJECT>
       <OBJECT ID="IDTextBox"
        CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3"
   STYLE="TOP:25pt;LEFT:8pt;WIDTH:99pt;HEIGHT:16pt;TABINDEX:3;ZINDEX:4;">
           <PARAM NAME="VariousPropertyBits" VALUE="746604571">
           <PARAM NAME="Size" VALUE="3493;564">
           <PARAM NAME="Value" VALUE="<%=Session("ID")%>">
           <PARAM NAME="FontCharSet" VALUE="0">
           <PARAM NAME="FontPitchAndFamily" VALUE="2">
       </OBJECT>
       <OBJECT ID="CommandButton1"
        CLASSID="CLSID:D7053240-CE69-11CD-A777-00DD01143C57"
   STYLE="TOP:50pt;LEFT:8pt;WIDTH:99pt;HEIGHT:25pt;TABINDEX:2;ZINDEX:2;">
           <PARAM NAME="Caption" VALUE="Get Item">
           <PARAM NAME="Size" VALUE="3493;882">
           <PARAM NAME="FontCharSet" VALUE="0">
           <PARAM NAME="FontPitchAndFamily" VALUE="2">
           <PARAM NAME="ParagraphAlign" VALUE="3">
       </OBJECT>
       <OBJECT ID="NameLabel"
        CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0"
   STYLE="TOP:8pt;LEFT:116pt;WIDTH:99pt;HEIGHT:17pt;ZINDEX:3;">
           <PARAM NAME="Caption" VALUE="Company Name">
           <PARAM NAME="Size" VALUE="3493;600">
           <PARAM NAME="FontCharSet" VALUE="0">
           <PARAM NAME="FontPitchAndFamily" VALUE="2">
       </OBJECT>
       <OBJECT ID="NameTextBox"
       CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3"
   STYLE="TOP:25pt;LEFT:116pt;WIDTH:99pt;HEIGHT:16pt;TABINDEX:1;ZINDEX:1;">
           <PARAM NAME="VariousPropertyBits" VALUE="746604571">
           <PARAM NAME="Size" VALUE="3493;564">
           <PARAM NAME="Value" VALUE="<%=Session("Name")%>">
           <PARAM NAME="FontCharSet" VALUE="0">
           <PARAM NAME="FontPitchAndFamily" VALUE="2">
       </OBJECT>
   </DIV>
  
When Page1.asp is requested from a browser. the following sequence of events occurs.
  1. Browser requests Page1.asp from IIS.


  2. IIS receives the request and recognizes the page as a script page.


  3. IIS processes the script page.


  4. In processing the script, a connection is stored as a session variable.


  5. An initial query is performed, and two more session variables holding the CustomerID and the CustomerName are created.


  6. IIS passes the resulting HTML code from Page1.asp back to the browser.


  7. The browser begins displaying the page until it reaches the OBJECT tag that points to Query.asp.


  8. The browser requests Query.asp from IIS.


  9. IIS parses the Query.asp script tags and places the Name and ID into the text boxes in the layout.


  10. IIS passes the Query.asp back to the browser, which interprets the file as a layout file (ALX).


  11. The ALX appears, and the browser now waits for user input.


  12. The user can input an ID into the first text box, and click Command to perform the search.


  13. The Command button calls VBScript, which calls a procedure in Page1.asp.


  14. The procedure in Page1.asp populates a hidden form and then submits the form to itself (Page1.asp).


  15. Page1.asp is run as a script again.


  16. Page1.asp recognizes that it has been called by a form and constructs a SQL query based on information passed.


  17. It then populates the session variables ID and Name.


  18. The page is passed back to the browser, which displays it, and again requests the Query.asp layout page.


  19. Query.asp is parsed as a script and returns a valid ALX layout file.


  20. The results appear on the screen.


NOTE: This example requires the AdvWorks data source set up during the Active Server Pages installation.


REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

Q157748 Modifying Objects in an ActiveX Layout

For the latest Knowledge Base articles and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:
http://support.microsoft.com/support/vinterdev/

Additional query words: 1.00 kbdsi


Keywords          : kbActiveX kbADO kbASP kbASPObj kbScript kbGrpASP 
Version           : winnt:
Platform          : winnt 
Issue type        : kbhowto 

Last Reviewed: May 27, 1999