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

Slides:



Advertisements
Similar presentations
Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In.
Advertisements

Zhongxing Telecom Pakistan (Pvt.) Ltd
Advanced SQL Topics Edward Wu.
Managing Multi-User Databases (2) IS 240 – Database Management Lecture #19 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Chapter 1 The Study of Body Function Image PowerPoint
BASIC SKILLS AND TOOLS USING ACCESS
Structured Query Language (SQL)
Chapter 3: Linked List Tutor: Angie Hui
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Tutorial 3 – Creating a Multiple-Page Report
XP New Perspectives on Microsoft Office Word 2003 Tutorial 6 1 Microsoft Office Word 2003 Tutorial 6 – Creating Form Letters and Mailing Labels.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
ABC Technology Project
South Dakota Library Network ALEPH v20 Tables © South Dakota Library Network, 2012 ©Ex Libris (USA), 2009 Modified for SDLN Version Last Update:
MySQL Access Privilege System
1 Web-Enabled Decision Support Systems Access Introduction: Touring Access Prof. Name Position (123) University Name.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
© Paradigm Publishing, Inc Access 2010 Level 1 Unit 1Creating Tables and Queries Chapter 2Creating Relationships between Tables.
Microsoft Office Illustrated Fundamentals Unit K: Working with Data.
Microsoft Access.
Displaying Data from Multiple Tables
State of Connecticut Core-CT Project Query 8 hrs Updated 6/06/2006.
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
Introducing WebDewey 2.0. Introducing WebDewey 2.0.
Well-Being Icon Refer to Slide 2 for instructions on how to view the full-screen slideshow.Slide 2.
1 University of Utah – School of Computing Computer Science 1021 "Thinking Like a Computer"
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Green Eggs and Ham.
VOORBLAD.
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
Benchmark Series Microsoft Excel 2013 Level 2
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
4 Oracle Data Integrator First Project – Simple Transformations: One source, one target 3-1.
© 2012 National Heart Foundation of Australia. Slide 2.
Connecting to Database 21 Feb Database Options Can use many different databases in conjunction with php. – MySql; MS Access; Oracle; etc etc Most.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
25 seconds left…...
Januar MDMDFSSMDMDFSSS
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
CpSc 3220 Designing a Database
Chapter 8 Improving the User Interface
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
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
CIS 451: ASP Recordsets Dr. Ralph D. Westfall May, 2002.
ASP & ADO. Connection Object An implicit connection is created when we open a recordset without a connection object. –rs.open “Customer”, "DSN = Sales”
ActiveX Data Object (ADO) in JavaScript J.L.Wang, Yen-Cheng Chen Dept. of Infomation Management Ming-Chuan University Jan
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.
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.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
 2 Data Object Library approaches ◦ DAO (Data Access Objects)  Original access strategy (up to VB6)  Closely linked to MS Access ◦ ADO (ActiveX Data.
ADO VBA Programming in Access
ActiveX Data Objects (ADO)
INT213 Updating the Database.
Presentation transcript:

ADO DB in Access VBA © Walter Milner 2005 Slide: 1 ADO VBA Programming in Access

ADO DB in Access VBA © Walter Milner 2005 Slide: 2 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 3 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 4 Why its not simple 3 The actual data (and server) might not be a relational database Could be a web page or spreadsheet

ADO DB in Access VBA © Walter Milner 2005 Slide: 5 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 6 Fundamental objects Connection Recordset

ADO DB in Access VBA © Walter Milner 2005 Slide: 7 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 8 Recordset A recordset is just a set of records (rows) Open a recordset (through a connection) Do something with the rows Close the recordset

ADO DB in Access VBA © Walter Milner 2005 Slide: 9 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 teh IDs

ADO DB in Access VBA © Walter Milner 2005 Slide: 10 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 11 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 12 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 13 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.Recordset (works matching one field only)

ADO DB in Access VBA © Walter Milner 2005 Slide: 14 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 Finding a record - example 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 15 Find record exercise Use the above to find and display values

ADO DB in Access VBA © Walter Milner 2005 Slide: 16 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 Altering data - upDate

ADO DB in Access VBA © Walter Milner 2005 Slide: 17 UpdateBatch myTableRS.Open "myTable", conn, adOpenStatic, adLockOptimistic myTableRS.MoveFirst Do While Not myTableRS.EOF myTableRS.Fields("PhoneNumber") = myTableRS.Fields("PhoneNumber") + 1 myTableRS.MoveNext Loop myTableRS.UpdateBatch

ADO DB in Access VBA © Walter Milner 2005 Slide: 18 Practice with update Try using update as above Try updatebatch Combine find with update to change selected records only – –in a loop have a sequence of –find –update

ADO DB in Access VBA © Walter Milner 2005 Slide: 19 Inserting new rows.. myTableRS.Open "myTable", conn, adOpenDynamic, adLockPessimisticmyTableRS.AddNew nameTxtBox.SetFocus myTableRS.Fields("Name") = nameTxtBox.Text phoneTxtBox.SetFocus myTableRS.Fields("PhoneNumber") = phoneTxtBox.TextmyTableRS.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

ADO DB in Access VBA © Walter Milner 2005 Slide: 20 Deleting records.. IDTxtBox.SetFocus myTableRS.Find "ID = " & IDTxtBox.Text If Not myTableRS.EOF Then myTableRS.Delete myTableRS.Delete myTableRS.Update 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 21 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 22 SQL practice Use the above approach to debug.print data from 2 JOINed tables

ADO DB in Access VBA © Walter Milner 2005 Slide: 23 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" myCommand Dim myCommand As ADODB.command Set myCommand = New ADODB.command ActiveConnection myCommand.ActiveConnection = conn CommandText myCommand.CommandText = "Update myTable set phonenumber=phonenumber + 2" Execute myCommand.Execute conn.Close Set conn = Nothing Here commandtext is SQL update statement No recordset needed Try it

ADO DB in Access VBA © Walter Milner 2005 Slide: 24 Command, Recordset and FlexGrid MS FlexGrid not standard control Get it by 'More controls' on toolbox

ADO DB in Access VBA © Walter Milner 2005 Slide: 25 FlexGrid 1 – get the data 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 = "select * from myTable" Dim rs As ADODB.Recordset Set rs = myCommand.Execute command returns a recordset

ADO DB in Access VBA © Walter Milner 2005 Slide: 26 FlexGrid2 – setting it up Dim fieldCount As Integer fieldCount = rs.Fields.count MSFlexGrid1.Cols = fieldCount + 1 MSFlexGrid1.AllowUserResizing = flexResizeColumns MSFlexGrid1.Rows = 50 For i = 0 To fieldCount - 1 MSFlexGrid1.TextMatrix(0, i + 1) = rs.Fields(i).Name Next set number of columns – 1 more than field count put fieldnames into top row

ADO DB in Access VBA © Walter Milner 2005 Slide: 27 FlexGrid 3 – recordset -> grid rs.MoveFirst count = 1 Do While Not rs.EOF MSFlexGrid1.TextMatrix(count, 0) = count For i = 0 To fieldCount - 1 MSFlexGrid1.TextMatrix(count, i + 1) = rs.Fields(i) Next count = count + 1 rs.MoveNext Loop rs.Close for each record.. put record number at left. for each field in row. place field value in grid

ADO DB in Access VBA © Walter Milner 2005 Slide: 28 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 29 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)

ADO DB in Access VBA © Walter Milner 2005 Slide: 30 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

ADO DB in Access VBA © Walter Milner 2005 Slide: 31 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