0% found this document useful (0 votes)
289 views4 pages

ADO Code Examples for MS Access

This document provides code examples for using ADO (ActiveX Data Objects) to access and manipulate data in a Microsoft Access database. It includes functions that list tables, open recordsets, create and modify queries, and execute action queries. ADO allows some operations that are not supported by DAO, but requires references to additional libraries like ADOX for full database manipulation capabilities.

Uploaded by

JigneshShah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
289 views4 pages

ADO Code Examples for MS Access

This document provides code examples for using ADO (ActiveX Data Objects) to access and manipulate data in a Microsoft Access database. It includes functions that list tables, open recordsets, create and modify queries, and execute action queries. ADO allows some operations that are not supported by DAO, but requires references to additional libraries like ADOX for full database manipulation capabilities.

Uploaded by

JigneshShah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7/16/2014 Microsoft Access tips: ADO Programming Code Examples

[Link] 1/4
Microsoft Access: ADO Programming Code Examples
Provided by Allen Browne, March 2007. Updated May 2009.
ADO Programming Code Examples
This 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 of
databases other than Access. In the wider world beyond Access, ADO has largely been replaced by the quite different [Link]
library.
In general, DAO is preferred over ADO, but there are some operations that work under ADO only. In general, these work in code
only. They will not work if you try them in the Query window, since Access itself uses DAO. They also require JET 4 (Access 2000 or
later.)
ADO provides only limited ways to manipulate the data structure (typically via DDL query statements), unless you also use the ADOX
library 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 Database
Option Explicit
Function ShowSchema()
'Purpose: List the tables, using ADO.
Dim cn As [Link]
Dim rs As [Link]
Dim i As Integer

Set cn = [Link]
Set rs = [Link](adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))
' For i = 0 To [Link] - 1
' [Link] [Link](i).Name
' Next
Do While Not [Link]
[Link] [Link]("TABLE_NAME").Value
[Link]
Loop
7/16/2014 Microsoft Access tips: ADO Programming Code Examples
[Link] 2/4
[Link]

Set rs = Nothing
Set cn = Nothing
End Function
Function AdoRecordsetExample()
'Purpose: Open a recordset using ADO.
Dim rs As New [Link]
Dim strSql As String

strSql = "SELECT MyField FROM MyTable;"
[Link] strSql, [Link]

Do While Not [Link]
[Link] rs!MyField
[Link]
Loop

[Link]
Set rs = Nothing
End Function
Function CreateViewAdo()
'Purpose: Create a new query using ADO.
Dim cn As [Link]
Dim strSql As String

strSql = "CREATE VIEW MyTableView AS SELECT MyTable.* FROM MyTable;"
Set cn = [Link]
[Link] strSql

[Link] "MyTableView created"
Set cn = Nothing
End Function
Function ModifyViewAdo()
'Purpose: Modify a query using ADO.
Dim cn As [Link]
Dim strSql As String

strSql = "ALTER TABLE Query1 AS SELECT MyTable.* FROM MyTable;"
Set cn = [Link]
[Link] strSql

[Link] "MyTableView modified"
Set cn = Nothing
End Function
Function ShowBand()
Dim rs As New [Link]
[Link] "SELECT (2 BAND 4) AS Result;", [Link]
ShowBand = rs!Result
[Link]
Set rs = Nothing
End Function
Function TestBnot()
'Purpose: Illustrate BNOT (binary NOT) operator (ADO only.)
Dim cn As [Link]
Dim strSql As String
Dim lngKt As Long

Set cn = [Link]
strSql = "UPDATE MyTable SET MyIntFlip = BNOT MyInt WHERE MyIntFlip Is Not Null;"
7/16/2014 Microsoft Access tips: ADO Programming Code Examples
[Link] 3/4

[Link] strSql, lngKt

Set cn = Nothing
TestBnot = lngKt
End Function
Function TestBand()
'Purpose: Illustrate BAND (binary AND) operator. (ADO only.)
Dim rs As New [Link]
Dim strSql As String

strSql = "SELECT MyBitField, (MyBitField BAND 2) <> 0 As MyResult FROM MyTable;"
[Link] strSql, [Link]

Do While Not [Link]
[Link] rs!MyBitfield, rs!MyResult
[Link]
Loop

[Link]
Set rs = Nothing
End Function
Function ShowUserRosterMultipleUsers()
'Source: kb 198755.
Dim cn As New [Link]
'Dim cn2 As New [Link]
Dim rs As New [Link]
Dim i, j As Long
[Link] = "[Link].4.0"
[Link] "Data Source=C:\Data\[Link]"
'[Link] "Provider=[Link].4.0;" & "Data Source=C:\Data\[Link]"
' 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 = [Link](adSchemaProviderSpecific, , "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
'Output the list of all users in the current database.
[Link] [Link](0).Name, "", [Link](1).Name, "", [Link](2).Name, [Link](3).Name
While Not [Link]
[Link] [Link](0), [Link](1), [Link](2), [Link](3)
[Link]
Wend
End Function
Function UserCount() As Long
Dim cnLocal As [Link] 'Current project connection.
Dim cnBackEnd As New [Link] 'Connection to back end database.
Dim rsBEUserRoster As New [Link] 'JET User Roster for back end database.
Dim rsTarget As New [Link] '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\[Link]"

7/16/2014 Microsoft Access tips: ADO Programming Code Examples
[Link] 4/4
'Open the JET User Roster for the back end.
[Link] = "[Link].4.0"
[Link] "Data Source=" & strPath
Set rsBEUserRoster = [Link](adSchemaProviderSpecific, , _
"{947bb102-5d43-11d1-bdbf-00c04fb92675}")

'Clear temp table, and copy the user roster in.
dtEnteredOn = Now()
Set cnLocal = [Link]
[Link] "DELETE FROM tzJetUserRoster;"
[Link] "tzJetUserRoster", cnLocal, adOpenDynamic, adLockOptimistic
Do While Not [Link]
[Link]
For lngKt = 0 To 3
rsTarget(lngKt) = rsBEUserRoster(lngKt)
rsTarget!EnteredOn = dtEnteredOn
Next
[Link]
[Link]
Loop
[Link]
[Link]
[Link]

'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 [Link]
[Link] strSql, cnLocal, adOpenKeyset
If Not ([Link] And [Link]) Then
[Link]
UserCount = [Link]
End If
[Link]

'Dereference objects
Set rsTarget = Nothing
Set rsBEUserRoster = Nothing
Set cnLocal = Nothing
Set 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 String
Dim lngKt As Long

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

ExecuteADO = lngKt
End Function
Home Index of tips Top

You might also like