ADO VBA Programming in Access
ADO VBA Programming in Access
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 teh IDs
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: 11
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)
Dim conn As ADODB.Connection Dim myTableRS As ADODB.Recordset Set conn = New ADODB.Connection Find a row with a certain key Set myTableRS = New ADODB.Recordset field value and display other conn.Provider = "Microsoft.Jet.OLEDB.4.0" field 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 Get required value from a text box
Do the Find
Display result
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
ADO DB in Access VBA Walter Milner 2005 Slide: 16
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
Deleting records
.. IDTxtBox.SetFocus myTableRS.Find "ID = " & IDTxtBox.Text If Not myTableRS.EOF Then This deletes a row (first one ) whose myTableRS.Delete ID field matches text box input myTableRS.Update .delete deletes current row MsgBox ("Record deleted") after update Else MsgBox ("No matching record") Try adapting to code to delete all matching records End If myTableRS.Close ..
ADO DB in Access VBA Walter Milner 2005 Slide: 20
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
Try it
ADO DB in Access VBA Walter Milner 2005 Slide: 23
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
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
ADO DB in Access VBA Walter Milner 2005 Slide: 31