ADO VBA Programming in Access

Slides:



Advertisements
Similar presentations
ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
ASP Application Development Session 3. Topics Covered Using SQL Statements for: –Inserting a tuple –Deleting a tuple –Updating a tuple Using the RecordSet.
ActiveX Data Object ISYS 562. ADO An ActiveX control ActiveX is build upon COM, a contract that defines a standard interface by which objects communicate.
Using Objects and Properties
Programming the RecordSet Object
Visual Basic Database Access BICS546. Microsoft Universal Data Access OLE DB: The OLE database protocol –Allows a program to access information in any.
CIS 451: ASP Recordsets Dr. Ralph D. Westfall May, 2002.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”
ADO Recordsets. Recordset Objects Similar to Tables and Queries: data Using VBA/VBScript you… –Open a recordset, –Locate a record –Update or add a record.
Interacting With Data Week 8 Connecting to the database Creating recordsets Interacting with the database.
Interacting With Data Databases.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan
Introduction to ADO By David R. Stevenson Consulting Software Engineer ABB Automation.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Faculty of Electronic Engineering 1 Database Access API’s Aleksandar Stanimirović Leonid Stoimenov Aleksandar Milosavljević.
Lecture Note 10: Simple Database Techniques. Introduction –Database System –Access, SQL Server and others. –Microsoft Access - Interacting with this databases.
Visual Basic ADO Programming 56:150 Information System Design.
What is database?  Any Method for access info into Application from DataBase?  ODBC is standard for Accessing Data.  Problem with ODBC:  Information.
How to Connect to Database ODBC (Open Database Connectivity) ADO (ActiveX Data Object) ASP Code To Connect to Database Recordset Object Navigating through.
ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard set of objects to refer.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Chapter 9 Building the Shopping cart Objective Creating Shopping cart using session Variable. Creating a shopping cart using a database table. Use the.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Lab 8 Data Access Using Microsoft ActiveX Data Object (ADO)
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Database Connectivity What is ADO. What is ADO? ADO is a Microsoft technology ADO stands for ActiveX Data Objects ADO is a Microsoft Active-X component.
ADO ActiveX Data Object. ActiveX Data Objects (ADO) is Microsoft’s latest database object model. The goal of ADO is to allow VB developers to use a standard.
Source = Table rsObject.Open tablename, Connection Object, CursorType, LockType, adCmdTable Source = Stored Procedure rsObject.Open stored procedure name,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 6: Accessing a database with PHP Rob Gleasure robgleasure.com.
Using a Database Access97 Please use speaker notes for additional information!
Labtest.ASP Notes. INSERT STATUS INSERT STATUS
ASP-13-1 Recordsets Colorado Technical University IT420 Tim Peterson.
1 ADO Activex Data Objects. 2 ADO ADO allows users to access data easily from many existing databases (such as Access or Paradox) From ODBC compliant.
ADO & Recordsets. ADO Description & History  ActiveX Data Objects  History  1991 ODBC  Data Access Objects (DAO) for VB developers (JET)  Remote.
Using databases ActiveX Data Objects (ADO) Connecting to a database Reading data from a database Inserting, updating and deleting records Using databases.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
Emdeon Office Batch Management Services This document provides detailed information on Batch Import Services and other Batch features.
JSP and DB.
ASP.NET Programming with C# and SQL Server First Edition
Visual Basic Database Access
Visual Basic Database Programming.
Y.-H. Chen International College Ming-Chuan University Fall, 2004
Running a Forms Developer Application
Microsoft Office Access 2010 Lab 3
Find, filter etc with connection to Access code internally
Practical Office 2007 Chapter 10
ActiveX Data Objects (ADO)
INT213 Updating the Database.
Introduction to Microsoft Access
Unit 8.2 How data can be used Accessing Data in Views
5. Using databases in VB.
Microsoft Access 2003 Illustrated Complete
Error Handling Summary of the next few pages: Error Handling Cursors.
ربط الفيجوال بيسك VB مع قواعد البيانات
ISC440: Web Programming 2 Server-side Scripting PHP 3
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
Chapter 10 ASP and Data Store Access
Developing a Model-View-Controller Component for Joomla Part 3
This allows me to insert data for specified fields and not for other fields in the structure.
Tutorial 6 PHP & MySQL Li Xu
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

ADO VBA Programming in Access

Why its not simple 1 MS Access has a 'built-in' database engine called Jet – which you might use But you might instead use a separate data server Like MS SQLServer, Oracle or MySQL These work slightly differently

Why its not simple 2 The actual data (and server) might be on the same machine that Access is running on But it might not

Why its not simple 3 The actual data (and server) might not be a relational database Could be a web page or spreadsheet

So ADO = Active Data Objects is a single object model to cover all cases therefore pretty intricate (but can be simple) Here we only cover – running from VBA in Access using a local Access database

Fundamental objects Connection Recordset

Connection Represents a single session with a data provider. The sequence is – Set up connection Open connection Do things with the data Close the connection

Recordset A recordset is just a set of records (rows) Open a recordset (through a connection) Do something with the rows Close the recordset

Simple example An Access database has a table called myTable and a key field called ID The following code (in a button on a form) goes through the table and displays all the IDs

Simple example : part 1 'declare conn to be a Connection - Dim conn As ADODB.Connection ' make a connection object - Set conn = New ADODB.Connection ' specify what kind of data provider it is - conn.Provider = "Microsoft.Jet.OLEDB.4.0" ' open the connection on one database - conn.Open "c:/walter/ass21.mdb" ' declare a recordset - Dim myTableRS As ADODB.Recordset ' make one - Set myTableRS = New ADODB.Recordset ' open it using a table in the database, and the connection myTableRS.Open "myTable", conn, adOpenDynamic, adLockPessimistic

Simple example : 2 ' go to start of recordset - myTableRS.MoveFirst ' until we reach the end.. Do Until myTableRS.EOF ' display the ID field in current row MsgBox (myTableRS.Fields("ID")) ' move next row myTableRS.MoveNext Loop 'close the recordset myTableRS.Close Set myTableRS.ActiveConnection = Nothing ' and the connection conn.Close Set conn = Nothing

Reading a table Make a database and a table with a numeric field and a text field. Put in a few rows. Write a routine like the above example, to total the numeric field and display it with a MsgBox

Find Find Method (from Microsoft Help file..) Searches a Recordset for the row that satisfies the specified criteria. Optionally, the direction of the search, starting row, and offset from the starting row may be specified. If the criteria is met, the current row position is set on the found record; otherwise, the position is set to the end (or start) of the Recordset. (works matching one field only)

Finding a record - example Dim conn As ADODB.Connection Dim myTableRS As ADODB.Recordset Set conn = New ADODB.Connection Set myTableRS = New ADODB.Recordset conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.Open "c:/walter/ass21.mdb" myTableRS.Open "myTable", conn, adOpenStatic, adLockOptimistic Dim wanted As String Text5.SetFocus wanted = Text5.Text myTableRS.Find "ID = " & wanted If Not myTableRS.EOF Then Label8.Caption = myTableRS.Fields("Name") Else Label8.Caption = "Not found" End If Find a row with a certain key field value and display other field Get required value from a text box Do the Find Display result

Find record exercise Use the above to find and display values

Altering data - upDate Dim conn As ADODB.Connection Dim myTableRS As ADODB.Recordset Set conn = New ADODB.Connection Set myTableRS = New ADODB.Recordset conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.Open "c:/walter/ass21.mdb" myTableRS.Open "myTable", conn, adOpenStatic, adLockOptimistic myTableRS.MoveFirst Do While Not myTableRS.EOF myTableRS.Fields("PhoneNumber") = myTableRS.Fields("PhoneNumber") + 1 myTableRS.Update myTableRS.MoveNext Loop myTableRS.Close Set myTableRS.ActiveConnection = Nothing conn.Close

Practice with update Try using update as above Combine find with update to change selected records only – in a loop have a sequence of find update

Inserting new rows .. myTableRS.Open "myTable", conn, adOpenDynamic, adLockPessimistic myTableRS.AddNew nameTxtBox.SetFocus myTableRS.Fields("Name") = nameTxtBox.Text phoneTxtBox.SetFocus myTableRS.Fields("PhoneNumber") = phoneTxtBox.Text myTableRS.Update myTableRS.Close New record is added at the end of the table In a relational database, record order has no significance Try this out Try using adLockReadOnly as the lock type

Deleting records .. IDTxtBox.SetFocus myTableRS.Find "ID = " & IDTxtBox.Text If Not myTableRS.EOF Then myTableRS.Delete myTableRS.Update MsgBox ("Record deleted") Else MsgBox ("No matching record") End If myTableRS.Close This deletes a row (first one ) whose ID field matches text box input .delete deletes current row after update Try adapting to code to delete all matching records

Using SQL as recordset source myTableRS.Open "Select ID, name From myTable", conn, adOpenDynamic, adLockPessimistic Do While Not myTableRS.EOF For i = 1 To myTableRS.Fields.Count Debug.Print myTableRS.Fields(i - 1), Next Debug.Print myTableRS.MoveNext Loop

SQL practice Use the above approach to debug.print data from 2 JOINed tables

Command object Dim conn As ADODB.Connection Set conn = New ADODB.Connection conn.Provider = "Microsoft.Jet.OLEDB.4.0" conn.Open "c:/walter/ass21.mdb" Dim myCommand As ADODB.command Set myCommand = New ADODB.command myCommand.ActiveConnection = conn myCommand.CommandText = "Update myTable set phonenumber=phonenumber + 2" myCommand.Execute conn.Close Set conn = Nothing Here commandtext is SQL update statement No recordset needed Try it

RecordSet object things - cursor The cursor is the 'current row' There are different kinds of cursors with different effects You select the cursor type before opening the recordset

Cursor types Static. Is snapshot – changes by other users are invisible. adOpenStatic ForwardOnly. Like the above but you can only move forward through rows – more efficient. adOpenForwardOnly Dynamic. Changes by others seen, move anywhere. adOpenDynamic Keyset. Like dynamic, but can't see rows added by others. adOpenKeyset (but you don't always get this – it depends on the way the recordset is generated)

Data Locking Danger – 2 users processing the same data at the same time might over-write each others work Solution – the first user puts a 'lock' on the data which prevents others using it at the same time

Types of lock adLockReadOnly - you are only reading records so they are not locked adLockPessimistic – record locked when you access it, released when finished adLockOptimistic – record only locked when you update it – might go wrong adLockBatchOptimistic - only locked when do batch update