473,467 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ


Updating Records

By Robert Murdock
Programmer, Darpac Inc.

Update A Record

The following example is the code on how to update or edit a record. The code will also check to see if the record already exists. It the record does not already exist, a new record will be added.




File: update.asp

<% 
 <!--#include file="adovbs.inc"-->
   TEMP = Request.form("Name")
   
   
   Set DataConn = Server.CreateObject("ADODB.Connection")
   Set RS = Server.CreateObject("ADODB.RecordSet")
   DataConn.Open "DBQ=" & Server.Mappath("cdemo.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};"
   
   ' Note: To keep the example simple I update ALL RECORDS with the name that
   ' is entered in the text box. This is OK for this example since you will not
   ' (?) allow duplicates.  In the real world you would assign a unique ID# to 
   ' each record (access can do this for you).  
   Set rsInfo = DataConn.Execute("SELECT * FROM tblContact WHERE Name = '" & TEMP & "'")
   
   If rsInfo.EOF Then
      
   SQL = "INSERT INTO tblContact"
   SQL = SQL + " (Name, Email, Phone) VALUES ('" & Request("Name") & "', '" & Request("Email") & "', '" & Request.Form("Phone") & "');"
   Set rsInfo = DataConn.Execute(SQL)
      
   Response.Write "<CENTER>Record Updated!<br>" 
    
   Else
   SQL = "UPDATE tblContact"
   SQL = SQL + " SET Name = '" & Request("Name") & "', Email= '" & Request("Email") & "', Phone= '" & Request.Form("Phone") & "'"
   SQL = SQL + " WHERE Name = '" & TEMP & "';"
   Set rsInfo = DataConn.Execute(SQL)
   
   RS.Close
   DataConn.Close
   
   Response.Write "<CENTER>Record Updated!<br></CENTER>"
   End If
   
   %> 

« Connect to Database Display Records »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.