Updating Records in a Database in ASP

Download the code

Below is a table called 'Members_tbl' with 3 rows of data which belongs to a database called 'Members.mdb'.

Order By

Running the code below will update the row where LastName is equal to Wall and change the value of FirstName to Mike rather than Michael.

Call our file 'update_record.asp'.

<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Updating records in a database table</title>
</head>
<body>
<%
'declare your variables
Dim connection, sSQL, sConnString

'declare SQL statement that will query the database
sSQL="Update Members_tbl SET FirstName='Mike' WHERE LastName='Wall'"

'create an ADO connection object
Set connection = Server.CreateObject("ADODB.connection")

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Members.mdb")

'Open the connection to the database
Connection.Open sConnString

'execute the SQL
Connection.Execute sSQL

Response.write "<div align='center'>The record has been updated.</div>"

'Now close the connection object
connection.Close
Set connection = Nothing
%>
</body>
</html>

Note also that we haven't used the the recordset object as we are not returning a recordset, we are simply updating records.

Previous: DELETE Records

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.