Categories

Tools

bitdefender

1&1 Web Hosting

How to read and filter data from an Access database with ASP, using ADODB.Connection and ADODB.Recordset


This example used a query, with a city typed from user, to filter the data in the Customers table.

Code:

<html>
<head>
  <title>List of Customers - MdmSoft</title>
  <style type="text/css">
  	body {
	font-family: "verdana";
	font-size:14px;
	}
  
  	#customers { 
	border:1px solid #777;
	border-collapse:collapse;		
	font-family: "verdana";
	font-size:14px;
	width:468px;
	}
	
	th {
	background-color:#CCCCCC; 
	color:#0066FF;
	text-align:left;
	}
	
	tr {
	border:1px solid #777;
	}
  </style>
</head>
<body>
<div>
	Enter the city and click on the button Search<br />
	<form  method="post"  action="customers.asp" >
		<input name="txbCity" type="text" size="62" maxlength="80">
		<input id="btnSearch" type="submit" value="Search">
	</form>
</div>
<div>
<%
	' Create a Connection
	Dim conn
	Set conn = Server.CreateObject("ADODB.Connection")
	
	' Open the connection
	conn.Open "driver={Microsoft Access Driver (*.mdb)}; dbq=" & Server.MapPath("App_Data/Northwind.mdb")
	
	' Create a Recordset
    Dim rsCustomers 
	set rsCustomers = server.createobject("ADODB.Recordset")
    
	' Get the value of the City
	Dim city
	city = Request.form("txbCity")
	
	Dim where
	If city = "" Then
		where = " "
	Else
		where = " WHERE City='" & city & "'"
	End If
	
	' Create a SQL query
	Dim Sql
	Sql = "SELECT * FROM spSelectCustomers" & where

	' Open the Recordset
	rsCustomers.open Sql, conn, 3 , 3
	
	' Create a HTML table
    Response.write( "<table id='customers' border='0'>" )
		Response.write( "<tr>")
			Response.write( "<th>Name</th>")
			Response.write( "<th>Company</th>")
			Response.write( "<th>City</th>")
		Response.write( "</tr>")
	
	' Loop the Recordset and print the information
	Do While not rsCustomers.EOF
		Response.write( "<tr>")
			Response.write( "<td>" & rsCustomers.Fields(0) & "</td>")
			Response.write( "<td>" & rsCustomers.Fields(1) & "</td>")
			Response.write( "<td>" & rsCustomers.Fields(2) & "</td>")
		Response.write( "</tr>")
		rsCustomers.MoveNext
	Loop
	
	Response.write( "</table>" )
		
	' Close all 
	rsCustomers.close
	conn.close
%>
</div>
</body>
</html>

Sample result:

How to read and filter data from an Access database with ASP

Posted in ASP by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.