In this following example our Access database is called 'Friends.mdb' and the table 'tblFriends'. The table has 3 fields firstly an 'ID' field that is an autonumber, secondly a field called 'FirstName' which is a textfield and lastly another textfield called 'SurName'. You can add some records into your table and also try the code without any records.
Our file is called 'records.asp'.
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Are there any records returned</title>
</head>
<body>
<%
'declare your variables
Dim connection, recordset
Dim SQL, sConnString
'declare SQL statement that will query
the database
SQL="SELECT * FROM tblFriends"
'define the connection string, specify
database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Friends.mdb")
'create an ADO connection and recordset
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
'Open the connection to the database
connection.Open sConnString
'Open the recordset object, execute the
SQL statement
recordset.Open SQL,connection
If recordset.eof then
response.write "There were no records returned."
Else
response.write "There are records returned."
End if
'close the connection and recordset objects
freeing up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>
</body>
</html>
Depending on whether you had records in the table you will either get the message 'There were no records returned.' or 'There are records returned'. It's all fairly straightforward.
| Previous: UPDATING Records |
Next: SELECT a
Random Record |
Get the best asp web hosting provider now and save 30%
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.