Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

Connect to the database

All the pieces are in place. Now you'll use the System DSN you created to connect to the Northwind.mdb sample database in \inetpub\wwwroot\sample the same directory where you stored the ASP example scripts. Open a browser and execute each ASP file by entering a URL in the form http://server/subfoldername/filename.asp where server is the name of your server, subfoldername identifies the folder within wwwroot containing the ASP file, and filename.asp is the name of the ASP file that you are executing. In this case, the server should be localhost, so to run the first example file enter http://localhost/sample/example.asp

The result should be a list of names, last then first, of each employee in the Northwind Employees table. Let's look at the actual scripts; all of the ASP code is enclosed in < % and %> tags. In example.asp, the first statement< %Set Conn = Server.CreateObject("ADODB.Connection") Creates a new instance of the connection object named Conn. You can give objects any name that you want, but descriptive names are recommended. By default the connection is read-only, which is what we want in this case. You can change this by adding a following line Conn.Mode = constant Where constant is adModeRead for read-only, adModeWrite for write-only, or adModeReadWrite to allow read and write.

Now you must open the connection using the System DSN that you created earlier. So the next statement Conn.Open "NorthWindDSN" Points the connection to the NorthwindDSN, which gives you access to the Northwind sample database in \inetpub\wwwroot\sample.The next statements

Sql = "SELECT LastName, FirstName FROM Employees"

set rst = Conn.Execute(sql)

Populate rst with the data from the LastName and FirstName fields in the Employees table. The While loop then moves through each record in rst, displaying the contents of the LastName and FirstName fields.

This simple example returns two data fields from every record in the table. You will rarely retrieve so much data and will more likely limit the retrieved information to some criteria. To illustrate, example2.asp restricts the SQL statement to retrieve only those records where the LastName field equals the string 'Reid':

Sql = "SELECT LastName, FirstName FROM employees WHERE LastName = 'Reid'"

The If statement checks rst for records, and the EOF property will be true if rst is empty. In which case, the code displays an error message, and Response.End Kills the script. Any ASP or HTML after this statement will not be executed.

Formatting tables

The first two ASP files deal with simple data retrieval. The third grabs the data from the LastName and FirstName fields in the Employees table and format it in a simple HTML table. The formatting begins with

<table border="1">

<caption align='"top"'>Employee List from Northwind. Demo</caption>

</table>

This starts the table and adds a title above it, while the statements


<tr>
<th>Last Name</th>
<th>First Name</th>
</tr>

Add headings to the table columns. The <tr></tr> and <td></td> tags create table rows and cells while the While loop populates it. The bgcolor="#00FFFF" attribute adds a bit of color to each row.

Click Next for modify-data-online page.

Share:

No comments:

Post a Comment

Translate



PageRank
Your email address:

Delivered by FeedBurner

Search

Infolinks Text Ads


Blogs Directory

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Labels

Forms How Tos Functions MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Queries External Links msaccess reports msaccess tips Menus and Toolbars Accesstips MsaccessLinks Process Controls Art Work Downloads msaccess How Tos Graph Charts msaccessQuery List Boxes Command Buttons Emails and Alerts Query Combo Boxes Custom Wizards DOS Commands ms-access functions msaccess graphs msaccess reporttricks msaccess functions msaccessprocess security advanced Access Security Data Macros Menus Property Report Top Values VBA msaccess email msaccess menus progressmeter Access2007 Array Auto-Number Command Button Copy Custom Functions Form Join Microsoft Numbering System Records Security Split SubForm Table Utility Workgroup database msaccess wizards Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting Calculation ChDir Color Palette Conditional Formatting Controls Data Filtering Data Type Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export Expression External Field Type Fields Filter Form Instances Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Methods Monitoring Object Reference Objects Octal Numbers Operating System Paste Primary-Key Product Rank Reading Recordset Rich Text Sequence SetFocus Summary Tab-Page Tables Time Difference Union Query User Users Variables Water-Mark Word automatically commands function hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar tutorial updating upload vba code

Featured Post

DIRectory and File Copy Utility

Last week we have seen how to use Dir() DOS Command , it’s ability to read files from the Disk  one by one and display it on the Debug Windo...

Labels

Blog Archive

Recent Posts