Microsoft Access
  Home arrow Microsoft Access arrow Page 4 - Updating Records in MS Access
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MICROSOFT ACCESS

Updating Records in MS Access
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2006-12-27

    Table of Contents:
  • Updating Records in MS Access
  • Form and Data Access Page based update
  • Updating data using the Update action query
  • Updating the column using ADO

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Updating Records in MS Access - Updating the column using ADO


    (Page 4 of 4 )

    While the earlier methods discussed the options available in the MS Access program for updating the records, records can also be updated programmatically using ADO. This can be carried out within the MS Access application, or from an independent Visual Basic, or an ASP application using the SQL update statement whose syntax is shown here.

    UPDATE "table_name"
    SET "column_1" = [new value]
    WHERE {condition}
    

    In order to access the properties, methods and events of ADO you need to add a reference to the ADO library as discussed in several earlier tutorials. Now to go to the load event of an MS Access form, type in the code shown in the next paragraph. Relevant sections which need some explanation are commented. Basically you should use the recordset object which takes the SQL update statement similar to the one produced by the MS Access query designer. You also display the field values before you run the update and again after you run the update in order to display the changes made. These are displayed using the debug statements.

    Private Sub Form_Load()
    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim strg As String
    strg = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:Documents and SettingsJayMy Documentscrud.mdb;" & _
    "Persist Security Info=False"
    Set conn = New ADODB.Connection
    Set rst = New ADODB.Recordset
    conn.Open strg
    MsgBox ("Open")
    Dim strsql As String
    strsql = "Select * from TestProducts where ProdID=10"
    rst.CursorLocation = adUseClient
    rst.Open strsql, conn, adOpenKeyset, adLockOptimistic
    MsgBox ("Cursor Location: " & rst.CursorLocation)
    Dim strresult As String
    strresult = ""
    strresult = strresult + "Prod ID= " & rst.Fields(0).Value & vbCrLf
    strresult = strresult + "Product Name= " & rst.Fields(1).Value & vbCrLf
    strresult = strresult + "Quantity Per Unit= " & rst.Fields(2).Value & 
    vbCrLf strresult = strresult + "Unit Price= " & rst.Fields(3).Value & vbCrLf strresult = strresult + "Unit In Stock= " & rst.Fields(4).Value & vbCrLf 'Printout the columns for ProductID=10 before updating the values Debug.Print strresult rst.Close 'Change product name to 'Japanese Tea' strupdate = "UPDATE TestProducts SET ProductName= 'Japanese Tea'
    WHERE ProdID=10" MsgBox (strupdate) 'Update the table changing column using update statement rst.Open strupdate, conn, adOpenKeyset, adLockOptimistic rst.Open strsql, conn, adOpenKeyset, adLockOptimistic 'reselect the same columns to verify if the change has taken place strnew = "" strnew = strnew + "Prod ID= " & rst.Fields(0).Value & vbCrLf strnew = strnew + "Product Name= " & rst.Fields(1).Value & vbCrLf strnew = strnew + "Quantity Per Unit= " & rst.Fields(2).Value & vbCrLf strnew = strnew + "Unit Price= " & rst.Fields(3).Value & vbCrLf strnew = strnew + "Unit In Stock= " & rst.Fields(4).Value & vbCrLf 'Print out the new values after the update has been made Debug.Print strnew rst.Close conn.Close End Sub

    When the form loads, the table gets updated and the result of the debug statements written to the immediate window are shown in the next paragraph.

    Product Name= Ikura
    Quantity Per Unit= 12 - 200 ml jars
    Unit Price= 31
    Unit In Stock= 31
    Prod ID= 10
    Product Name= Japanese Tea
    Quantity Per Unit= 12 - 200 ml jars
    Unit Price= 31
    Unit In Stock= 31
    

    Summary

    The MS Access query designer is a convenient tool for creating update action queries. It is also a good learning tool for studying data modifying SQL statements although the syntax is different. Browsing the data view of the table and making changes, Form and Data Access Pages based updates are other options. ADODB offers a programmatic way of modifying data which can use both types of CursorLocation property. Updating records using ADO enables you to use a lot more features than discussed here. Before you want to work with update queries make sure you consult the online help in MS Access and this Knowledge Base article on the Microsoft site.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · There are more ways than one to update records in an Access database. This article...
       · Jayaramji, Explanation of the topic is so nice and even novice can easily...
     

    MICROSOFT ACCESS ARTICLES

    - Linking SQL Express 2005 Tables to MS Access...
    - Working with Access Projects in Access 2007
    - Exploring Access 2007
    - Working with Stored Procedures in an MS Acce...
    - Creating and Using Action Queries
    - Creating Data Access Pages with Charts using...
    - Advanced Ideas using VBA
    - VBA Details
    - Updating Records in MS Access
    - Using ADO`s Record Object with URLs
    - Exporting XML from MS Access 2003
    - Importing XML into MS Access 2003
    - On Using Pass-through Queries in MS Access
    - Distributed Queries in MS Access
    - Configuring a Linked Microsoft Access Server...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway