4
M i c r o s o f t A c c e s s : A D O P r o g r a m m i n g C o d e E x a m p l e s P r o v i d e d b y A l l e n B r o w n e , M a r c h 2 0 0 7 . U p d a t e d M a y 2 0 0 9 . A D O P r o g r a m m i n g C o d e E x a m p l e s T h i s p a g e i s a r e f e r e n c e f o r d e v e l o p e r s , d e m o n s t r a t i n g h o w t o u s e t h e A D O l i b r a r y t o l i s t a n d m a n i p u l a t e t h e o b j e c t s i n A c c e s s . A D O ( A c t i v e X D a t a O b j e c t s ) i s m o r e g e n e r i c t h a n D A O ( t h e o n e d e s i g n e d t o h a n d l e t h e o b j e c t s i n A c c e s s ) , s o s u p p o r t s f e a t u r e s o f d a t a b a s e s o t h e r t h a n A c c e s s . I n t h e w i d e r w o r l d b e y o n d A c c e s s , A D O h a s l a r g e l y b e e n r e p l a c e d b y t h e q u i t e d i f f e r e n t A D O . N E T l i b r a r y . I n g e n e r a l , D A O i s p r e f e r r e d o v e r A D O , b u t t h e r e a r e s o m e o p e r a t i o n s t h a t w o r k u n d e r A D O o n l y . I n g e n e r a l , t h e s e w o r k i n c o d e o n l y . T h e y w i l l n o t w o r k i f y o u t r y t h e m i n t h e Q u e r y w i n d o w , s i n c e A c c e s s i t s e l f u s e s D A O . T h e y a l s o r e q u i r e J E T 4 ( A c c e s s 2 0 0 0 o r l a t e r . ) A D O p r o v i d e s o n l y l i m i t e d w a y s t o m a n i p u l a t e t h e d a t a s t r u c t u r e ( t y p i c a l l y v i a D D L q u e r y s t a t e m e n t s ) , u n l e s s y o u a l s o u s e t h e A D O X l i b r a r y w h i c h p r o v i d e s t h e e x t e n s i o n s t o g e t t o t h e d a t a b a s e c a t a l o g . T o u s e t h e A D O L i b r a r y , c h o o s e R e f e r e n c e s o n t h e T o o l s m e n u i n t h e c o d e w i n d o w , a n d c h e c k t h e b o x b e s i d e : M i c r o s o f t A c t i v e X D a t a O b j e c t s 2 . x L i b r a r y T h e r e i s n o e x p l a n a t i o n o f t h e c o d e b e y o n d i n - l i n e c o m m e n t s , a n d n o e r r o r h a n d l i n g i n m o s t e x a m p l e s . I n d e x o f F u n c t i o n s D e s c r i p t i o n S h o w S c h e m a ( ) L i s t t h e t a b l e s A d o R e c o r d s e t E x a m p l e ( ) O p e n a r e c o r d s e t C r e a t e V i e w A d o ( ) C r e a t e a n e w q u e r y M o d i f y V i e w A d o ( ) M o d i f y a q u e r y S h o w B a n d ( ) I l l u s t r a t e t h e B A N D o p e r a t o r w i t h l i t e r a l s . ( A D O o n l y . ) T e s t B n o t ( ) I l l u s t r a t e B N O T ( b i n a r y N O T ) o p e r a t o r ( A D O o n l y . ) T e s t B a n d ( ) I l l u s t r a t e B A N D ( b i n a r y A N D ) o p e r a t o r . ( A D O o n l y . ) S h o w U s e r R o s t e r M u l t i p l e U s e r s ( ) L i s t t h e u s e r s c u r r e n t l y c o n n e c t e d t o t h e d a t a b a s e . U s e r C o u n t ( ) C o u n t t h e n u m b e r o f d i s t i n c t u s e r s c o n n e c t e d t o t h e d a t a b a s e . E x e c u t e A D O ( ) E x e c u t e a n a c t i o n q u e r y w i t h A D O , a n d k n o w h o w m a n y r e c o r d s w e r e i n s e r t e d / d e l e t e d / c h a n g e d . O p t i o n C o m p a r e D a t a b a s e O p t i o n E x p l i c i t F u n c t i o n S h o w S c h e m a ( ) ' P u r p o s e : L i s t t h e t a b l e s , u s i n g A D O . D i m c n A s A D O D B . C o n n e c t i o n D i m r s A s A D O D B . R e c o r d s e t D i m i A s I n t e g e r S e t c n = C u r r e n t P r o j e c t . C o n n e c t i o n S e t r s = c n . O p e n S c h e m a ( a d S c h e m a T a b l e s , A r r a y ( E m p t y , E m p t y , E m p t y , " T A B L E " ) ) ' F o r i = 0 T o r s . F i e l d s . C o u n t - 1 ' D e b u g . P r i n t r s . F i e l d s ( i ) . N a m e ' N e x t D o W h i l e N o t r s . E O F D e b u g . P r i n t r s . F i e l d s ( " T A B L E _ N A M E " ) . V a l u e r s . M o v e N e x t L o o p r s . C l o s e S e t r s = N o t h i n g S e t c n = N o t h i n g E n d F u n c t i o n F u n c t i o n A d o R e c o r d s e t E x a m p l e ( )

ADO Programming Code Examples

Embed Size (px)

DESCRIPTION

Ado programming example there's my example here

Citation preview

Page 1: ADO Programming Code Examples

Microsoft Access: ADO Programming Code Examples

Provided by Allen Browne, March 2007. Updated May 2009.

ADO Programming Code ExamplesThis page is a reference for developers, demonstrating how to use the ADO library to list and manipulate the objects in Access.

ADO (ActiveX Data Objects) is more generic than DAO (the one designed to handle the objects in Access), so supports features ofdatabases other than Access. In the wider world beyond Access, ADO has largely been replaced by the quite different ADO.NETlibrary.

In general, DAO is preferred over ADO, but there are some operations that work under ADO only. In general, these work in codeonly. They will not work if you try them in the Query window, since Access itself uses DAO. They also require JET 4 (Access 2000 orlater.)

ADO provides only limited ways to manipulate the data structure (typically via DDL query statements), unless you also use the ADOXlibrary which provides the extensions to get to the database catalog.

To use the ADO Library, choose References on the Tools menu in the code window, and check the box beside:Microsoft ActiveX Data Objects 2.x Library

There is no explanation of the code beyond in-line comments, and no error handling in most examples.

Index of Functions Description

ShowSchema() List the tables

AdoRecordsetExample() Open a recordset

CreateViewAdo() Create a new query

ModifyViewAdo() Modify a query

ShowBand() Illustrate the BAND operator with literals. (ADO only.)

TestBnot() Illustrate BNOT (binary NOT) operator (ADO only.)

TestBand() Illustrate BAND (binary AND) operator. (ADO only.)

ShowUserRosterMultipleUsers() List the users currently connected to the database.

UserCount() Count the number of distinct users connected to the database.

ExecuteADO() Execute an action query with ADO, and know how many records were inserted/deleted/changed.

Option Compare DatabaseOption Explicit

Function ShowSchema()'Purpose: List the tables, using ADO.Dim cn As ADODB.ConnectionDim rs As ADODB.RecordsetDim i As Integer

Set cn = CurrentProject.ConnectionSet rs = cn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))

' For i = 0 To rs.Fields.Count - 1' Debug.Print rs.Fields(i).Name' Next

Do While Not rs.EOFDebug.Print rs.Fields("TABLE_NAME").Valuers.MoveNext

Looprs.Close

Set rs = NothingSet cn = Nothing

End Function

Function AdoRecordsetExample()

Page 2: ADO Programming Code Examples

'Purpose: Open a recordset using ADO.Dim rs As New ADODB.RecordsetDim strSql As String

strSql = "SELECT MyField FROM MyTable;"rs.Open strSql, CurrentProject.Connection

Do While Not rs.EOFDebug.Print rs!MyFieldrs.MoveNext

Loop

rs.CloseSet rs = Nothing

End Function

Function CreateViewAdo()'Purpose: Create a new query using ADO.Dim cn As ADODB.ConnectionDim strSql As String

strSql = "CREATE VIEW MyTableView AS SELECT MyTable.* FROM MyTable;"Set cn = CurrentProject.Connectioncn.Execute strSql

Debug.Print "MyTableView created"Set cn = Nothing

End Function

Function ModifyViewAdo()'Purpose: Modify a query using ADO.Dim cn As ADODB.ConnectionDim strSql As String

strSql = "ALTER TABLE Query1 AS SELECT MyTable.* FROM MyTable;"Set cn = CurrentProject.Connectioncn.Execute strSql

Debug.Print "MyTableView modified"Set cn = Nothing

End Function

Function ShowBand()Dim rs As New ADODB.Recordsetrs.Open "SELECT (2 BAND 4) AS Result;", CurrentProject.ConnectionShowBand = rs!Resultrs.CloseSet rs = Nothing

End Function

Function TestBnot()'Purpose: Illustrate BNOT (binary NOT) operator (ADO only.)Dim cn As ADODB.ConnectionDim strSql As StringDim lngKt As Long

Set cn = CurrentProject.ConnectionstrSql = "UPDATE MyTable SET MyIntFlip = BNOT MyInt WHERE MyIntFlip Is Not Null;"

cn.Execute strSql, lngKt

Set cn = NothingTestBnot = lngKt

End Function

Function TestBand()'Purpose: Illustrate BAND (binary AND) operator. (ADO only.)Dim rs As New ADODB.RecordsetDim strSql As String

strSql = "SELECT MyBitField, (MyBitField BAND 2) <> 0 As MyResult FROM MyTable;"rs.Open strSql, CurrentProject.Connection

Page 3: ADO Programming Code Examples

Do While Not rs.EOFDebug.Print rs!MyBitfield, rs!MyResultrs.MoveNext

Loop

rs.CloseSet rs = Nothing

End FunctionFunction ShowUserRosterMultipleUsers()

'Source: kb 198755.Dim cn As New ADODB.Connection'Dim cn2 As New ADODB.ConnectionDim rs As New ADODB.RecordsetDim i, j As Long

cn.Provider = "Microsoft.Jet.OLEDB.4.0"cn.Open "Data Source=C:\Data\Northwind2003.mdb"

'cn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Data\Northwind2003.mdb"

' The user roster is exposed as a provider-specific schema rowset' in the Jet 4 OLE DB provider. You have to use a GUID to' reference the schema, as provider-specific schemas are not' listed in ADO's type library for schema rowsets

Set rs = cn.OpenSchema(adSchemaProviderSpecific, , "{947bb102-5d43-11d1-bdbf-00c04fb92675}")

'Output the list of all users in the current database.

Debug.Print rs.Fields(0).Name, "", rs.Fields(1).Name, "", rs.Fields(2).Name, rs.Fields(3).Name

While Not rs.EOFDebug.Print rs.Fields(0), rs.Fields(1), rs.Fields(2), rs.Fields(3)rs.MoveNext

WendEnd Function

Function UserCount() As LongDim cnLocal As ADODB.Connection 'Current project connection.Dim cnBackEnd As New ADODB.Connection 'Connection to back end database.Dim rsBEUserRoster As New ADODB.Recordset 'JET User Roster for back end database.Dim rsTarget As New ADODB.Recordset 'Temp table to record users and de-dupe.Dim strPath As String 'Full path to back end.Dim strSql As String 'SQL string.Dim lngKt As Long 'Loop controller.Dim dtEnteredOn As Date 'Current date and time.

'Set this to the full path of your back end database.strPath = "C:\Data\Northwind2003.mdb"

'Open the JET User Roster for the back end.cnBackEnd.Provider = "Microsoft.Jet.OLEDB.4.0"cnBackEnd.Open "Data Source=" & strPathSet rsBEUserRoster = cnBackEnd.OpenSchema(adSchemaProviderSpecific, , _

"{947bb102-5d43-11d1-bdbf-00c04fb92675}")

'Clear temp table, and copy the user roster in.dtEnteredOn = Now()Set cnLocal = CurrentProject.ConnectioncnLocal.Execute "DELETE FROM tzJetUserRoster;"rsTarget.Open "tzJetUserRoster", cnLocal, adOpenDynamic, adLockOptimisticDo While Not rsBEUserRoster.EOF

rsTarget.AddNewFor lngKt = 0 To 3

rsTarget(lngKt) = rsBEUserRoster(lngKt)rsTarget!EnteredOn = dtEnteredOn

NextrsTarget.UpdatersBEUserRoster.MoveNext

LooprsTarget.Close

Page 4: ADO Programming Code Examples

rsBEUserRoster.ClosecnBackEnd.Close

'Get the count of the number of distinct users who are connected.strSql = "SELECT DISTINCT Computer_Name FROM tzJetUserRoster WHERE Connected = True;"Set rsTarget = New ADODB.RecordsetrsTarget.Open strSql, cnLocal, adOpenKeysetIf Not (rsTarget.BOF And rsTarget.EOF) Then

rsTarget.MoveLastUserCount = rsTarget.RecordCount

End IfrsTarget.Close

'Dereference objectsSet rsTarget = NothingSet rsBEUserRoster = NothingSet cnLocal = NothingSet cnBackEnd = Nothing

End Function

Function ExecuteADO() As Long'Purpose: How to execute an action query with ADO.'Return: Number of records affected by action query.Dim strSql As StringDim lngKt As Long

strSql = "INSERT INTO tblClient (Surname, FirstName ) " & _"SELECT 'Smith' AS Surname, 'Jim' AS FirstName;"

CurrentProject.Connection.Execute strSql, lngKt

ExecuteADO = lngKtEnd Function

Home Index of tips Top