HOWTO: Display Records from an Access Database with ASP

ID: Q180814


The information in this article applies to:


SUMMARY

This article shows you how to create a Web interface that displays information from an Access 97 relational database using Visual InterDev.

There are four main steps in this process:

  1. Create the Web project.


  2. Add a Data Connection to Your project.


  3. Create an Active Server Pages (ASP) page and Add it to a Data Command Control.


  4. Write Code to Act on the Recordset Object Returned by the Data Command Control.



MORE INFORMATION

Follow the steps below to create an interface that reads data stored in an Access 97 database.

Step 1: Create the Web Project


  1. Launch Visual InterDev.


  2. On the File menu, click New.


  3. Select the Web Project Wizard from the list on the Projects tab.


  4. Enter MyDatabaseApp for the name of your Web project in the Project Name box (this is the name of your project stored on your remote workstation).


  5. In the Location box, enter a path or click the ellipsis (...) to browse your local workstation for a place to store your project and files.


  6. Click OK to continue to the next step.


  7. Type or select the server for your Web, and click Next.


  8. In Step 2 of the wizard, select "Create a new Web."


  9. Type DB for the name of your new Web. This helps to demonstrate the difference between a project and a Web.


  10. Clear "Enable full text searching for pages in this Web."


  11. Click Finish to create your new Web.


Step 2: Add a Data Connection to Your Project


  1. From the Project menu, point to Add to Project, and then click Data Connection.


  2. Click New on the File Data Source tab.


  3. Select "Microsoft Access Driver (*.mdb)" and click Next.


  4. Click Browse.


  5. By default, this places you in the Data Source folder located in the WinNT\ODBC directory on Windows NT or the Program Files\ Common Files\ODBC directory on Windows 95.


  6. In the File Name box, type MyDataSource and click Save. The full path to your new Data Source Name (DSN) file appears in the box.


  7. Click Next, then click Finish.


  8. In the "ODBC Microsoft Access 97 Setup" dialog box, click Select (located in the Database section of the dialog box).


  9. Browse or type in the path to the Adventure Works (AdvWorkds.mdb) database.

    NOTES:

    The Adventure Works example site is installed by default when you add Active Server Pages to Microsoft Internet Information Server. The site is installed by default into the InetPub\ASPSamp\AdvWorks directory.

    If your database resides on a machine other than the Microsoft Internet Information Server server, you may want to use a Universal Naming Convention (UNC) path so that the server can locate it on your network. Following is a sample UNC path:
    
          \\SERVERNAME\ROOTSHARENAME\InetPub\ASPSamp\AdvWorks\AdvWorks.mdb 

    SERVERNAME represents the name of your server and ROOTSHARENAME represents the share name of your root directory.

    Note also that when you use an Access 97 database, you must set the NTLM CHANGE permissions for the directory where the database is located. This means that the IUSR_<machinename> account, for anonymous user logon, must have CHANGE permissions for the entire directory.

    For additional information, please see the following article(s) in the Microsoft Knowledge Base:
    Q175804 PRB: Server Object Error 'ASP 0177:80040154' Server.CreateObject

    Q180815 BUG: IIS 4 Fails When Using a UNC to Access


  10. Click OK to select the database for your DSN.


  11. Click OK to complete the DSN setup.


  12. Select MyDataSource.dsn from the list in the Select Data Source dialog box.


  13. Click OK to complete the process.


Step 3: Create an ASP Page and Add It to a Data Command Control


  1. On the Project menu, point to Add to Project, and click New.


  2. Select Active Server Page on the Files tab.


  3. Type "Database" in the File Name box, and leave all other default options.


  4. Click OK to create the page.


  5. Leave the "<!-- Insert HTML here -->" line selected in the code window. On the Insert menu, point to Into HTML, and click ActiveX Control.


  6. Click the Design-time tab in the Insert ActiveX Control dialog box.


  7. Select the Data Command control from the list provided, and click OK. A Properties dialog box for your new control appears.


  8. Leave "DataCommand1" in the ID box, and select DataConn from the DataConnection list. This is the DataConnection that you created in step 2.


  9. Click SQL Builder to build a SQL statement that returns a Recordset.


  10. Click the plus sign (+) to expand the Tables tree in the Data View window.


  11. Drag the Products table into the Query Builder window.


  12. Select * (All Columns) in the Products table (located in the Query Builder window) to create the following SQL statement:
    
          SELECT Products.*
          FROM Products 


  13. On the File menu, click Close to close the Query Builder, and click Yes when prompted to update DataCommand1.

    You should see the new SQL statement in the Command Text box of the Properties dialog box.


  14. Close the DataCommand1 control window.

    Note that Visual InterDev creates the code necessary to execute the query against the Access database and to return the results in the Recordset object.


Step 4: Write Code to Act on the Recordset Object Returned by the Data

Command Control


  1. Place the following code after the <!--METADATA TYPE="DesignerControl" endspan--> tag in your ASP page:
    
          <%
            Dim fldTemp
            For Each fldTemp In DataCommand1.Fields
              Response.Write fldTemp.Value & "<BR>"
            Next
          %> 


  2. On the File menu, click Save to save the ASP page.


  3. Right-click the ASP page file name in the FileView window, and select "Preview in browser."



REFERENCES

For the latest Knowledge Base articles and other support information on Visual InterDev and Active Server Pages, link to the following Microsoft Technical Support site:

http://support.microsoft.com/support/vinterdev/


Keywords          : kbcode kbADO kbASP kbVisID kbVisID100 kbGrpASP 
Version           : Win95:1.0,3.0; WINNT:1.0,3.0
Platform          : Win95 winnt 
Issue type        : kbhowto 

Last Reviewed: May 4, 1999