Microsoft Access - Use ADO To Execute SQL Statements
Microsoft Access - Use ADO To Execute SQL Statements
Microsoft Access - Use ADO To Execute SQL Statements
1 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
EXCEL MODELS
Excel VBA
SERVICES
FEEDBACK
Search...
Excel &
VBA
EXCEL VBA
Excel Tips
Excel Functions
statements
Excel VBA
Excel Pivot Tables
User Rating:
Poor
/2
Best
Details
2/06/2014 7:45 PM
2 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
Query Language, is a computer language, and SQL statements are used to store,
retrieve and manipulate data in a relational database ex. Access Database.
A relational database has multiple tables, each table comprising of columns (fields)
and rows wherein data can be grouped using common attributes, and from which
data can be easily retrieved. The multiple tables relate to and work with each other
through certain key fields (like unique "ID Nos") viz. a separate table is created for
vendor-details and another table for purchase-orders wherein vendor details are not
repeated each time a purchase order is made and they are related with each other
through a unique "Vendor ID" column which is present in both tables. Whereas flat
file databases contain a single table of data with fields representing all parameters
viz. a single table will contain details of vendors and purchase orders made, and
with each purchase order the vendor details also get repeated resulting in
duplication. Flat file databases though easier to maintain, are far less robust and
flexible.
A Database Management System (DBMS) is a program which manages data in
databases, the manner in which data is created, stored, modified and accessed. A
Relational Database Management System (RDBMS) is the DBMS in respect of a
relational database. Microsoft Access is a popular RDBMS which supports creating
relational databases.
Other examples of DBMS are MySQL, Microsoft SQL Server, Oracle, etc.
In SQL, you can use DDL or DML statements. DDL (stands for Data Definition
Language) statements are used to make structural changes, to define the database
structure and objects like tables and columns. Examples of some often used DDL
commands are CREATE TABLE, DROP TABLE and ALTER TABLE. DML
statements are used to managing and manipulating data within the database
structure and deal with inserting, updating, deleteing and retrieving database
information like records in tables. Examples of often used DML commands are
include INSERT, SELECT, UPDATE and DELETE.
3 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
Image 3a
Image 3b
Sub automateAccessADO_2()
'In this example we show how to use ADO with SQL statements:
'1. Add and delete records using the ADO connection Execute method to
execute SQL statements;
'2. Select Records/Fields conditionally, using the ADO Recordset Open Method
with SQL statements.
'To use ADO in your VBA project, you must add a reference to the ADO Object
Library in Excel (your host application) by clicking Tools-References in VBE,
and then choose an appropriate version of Microsoft ActiveX Data Objects x.x
Library from the list.
2/06/2014 7:45 PM
4 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
'-------------'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String
Dim strSQL As String
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'-------------'THE CONNECTION OBJECT
strDBName = "SalesReport.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS
Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For
Access 2007 (.accdb database) use the ACE Provider:
"Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the
Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider =
Microsoft.ACE.OLEDB.12.0; data source=" & strDB
'-------------------'CREATE AND DELETE RECORDS
'Besides using ADO by itself for entering data (viz. using the AddNew
method of the Recordset object) as shown earlier, you can also use ADO
with SQL statements.
'Refer Image 3a for SalesManager Table in MS Access file
SalesReport.accdb; Image 3b shows the SalesManager Table after
adding new record (before it is deleted).
'add new records in the SalesManager Table using SQL INSERT INTO
statements, by specifying both the column names and the values to be
inserted. The Execute method on the specified ADO connection object,
executes the SQL statement passed in the CommandText argument:
connDB.Execute "INSERT INTO SalesManager (EmployeeId, FirstName,
Surname, JoinDate) VALUES (2, 'Mary', 'Smith', '6/20/2009')"
'alternatively add a new record by specifying only the column values to be
inserted:
'connDB.Execute "INSERT INTO SalesManager VALUES (2, 'Mary',
'Smith', '6/20/2009')"
'delete records in the SalesManager Table:
strSQL = "DELETE FROM SalesManager WHERE FirstName = 'Mary'"
connDB.Execute CommandText:=strSQL
'Value of 0 will be returned which indicates that the recordset is closed,
because it was never opened (value of 1 indicates that the recordset is
open):
MsgBox adoRecSet.State
'-------------------'SELECT RECORDS / FIELDS CONDITIONALLY
'Refer Image 3a for SalesManager Table in MS Access file
SalesReport.accdb, for below codes.
'select a specific field (FirstName), basis numeric reference:
2/06/2014 7:45 PM
5 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
6 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
adoRecSet.MoveNext
Loop
adoRecSet.Close
'-----------------'close the objects
connDB.Close
'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
End Sub
Image 4a
Image 4b
Sub automateAccessADO_3()
'In this example we show how to Edit Records, using ADO with SQL
statements:
'Refer Image 4a for SalesManager Table in MS Access file SalesReport.accdb
before edit, and Refer Image 4b after edit.
'To use ADO in your VBA project, you must add a reference to the ADO Object
Library in Excel (your host application) by clicking Tools-References in VBE,
and then choose an appropriate version of Microsoft ActiveX Data Objects x.x
Library from the list.
'-------------'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String
Dim strSQL As String
'instantiate an ADO object using Dim with the New keyword:
2/06/2014 7:45 PM
7 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
8 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
Image 5
Sub automateAccessADO_4()
'Use the OpenSchema Method to access information about the database
tables and columns.
'To use ADO in your VBA project, you must add a reference to the ADO Object
Library in Excel (your host application) by clicking Tools-References in VBE,
and then choose an appropriate version of Microsoft ActiveX Data Objects x.x
Library from the list.
'-------------'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'-------------'THE CONNECTION OBJECT
strDBName = "SalesReport.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS
Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For
Access 2007 (.accdb database) use the ACE Provider:
"Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the
Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider =
2/06/2014 7:45 PM
9 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
10 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
While using the SQL CREATE TABLE statement, you specify the Table Name, Field
Names and their Data Types. Commonly used data types for fields include: TEXT
(SIZE) for Text Field; CHAR (SIZE) for Text Field; SHORT for Numbers-Integer;
LONG for Numbers-Long; SINGLE for Numbers-Single; DOUBLE for NumbersDouble; CURRENCY for Currency; DATE for Date/Time; DATETIME for Date/Time;
YESNO for Boolean values; and so on.
Use the CONSTRAINT clause in SQL statements, to create a constraint on one or
more fields:
Specifying the PRIMARY KEY (can be only one in a Table) reserved word
designates a field(s) as a primary field whose values will be unique and cannot
be Null.
Specifying NOT NULL for a field will necessitate the new record to have valid
data in that field. A table column, by default, can have a NULL value ie. a field
can contain no value.
Specifying the UNIQUE reserved word will not allow a same value in that field,
for two records in the table.
Image 6
Sub automateAccessADO_5()
'Create a new database table using ADO with SQL statements.
'To use ADO in your VBA project, you must add a reference to the ADO Object
Library in Excel (your host application) by clicking Tools-References in VBE,
and then choose an appropriate version of Microsoft ActiveX Data Objects x.x
Library from the list.
'-------------'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'-------------'THE CONNECTION OBJECT
strDBName = "SalesReport.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS
Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For
2/06/2014 7:45 PM
11 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
12 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
Image 7
Sub automateAccessADO_6()
'Using the Find method to find or locate a specific record(s).
'To use ADO in your VBA project, you must add a reference to the ADO Object
Library in Excel (your host application) by clicking Tools-References in VBE,
and then choose an appropriate version of Microsoft ActiveX Data Objects x.x
Library from the list.
'-------------'DIM STATEMENTS
2/06/2014 7:45 PM
13 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
14 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
Loop
'2 records are found
MsgBox "Records found: " & i
'-------------'close the objects
connDB.Close
'destroy the variables
Set adoRecSet = Nothing
Set connDB = Nothing
End Sub
2/06/2014 7:45 PM
15 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
16 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
Image 8a
Image 8b
Sub automateAccessADO_8()
'Using ADO with SQL Statements to: Add, Delete and Modify Columns in an
Existing Table; Add and Delete Constraints on Columns.
'To use ADO in your VBA project, you must add a reference to the ADO Object
Library in Excel (your host application) by clicking Tools-References in VBE,
and then choose an appropriate version of Microsoft ActiveX Data Objects x.x
Library from the list.
'-------------'DIM STATEMENTS
Dim strMyPath As String, strDBName As String, strDB As String
Dim strSQL As String
'instantiate an ADO object using Dim with the New keyword:
Dim adoRecSet As New ADODB.Recordset
Dim connDB As New ADODB.Connection
'-------------'THE CONNECTION OBJECT
strDBName = "SalesReport.accdb"
strMyPath = ThisWorkbook.Path
strDB = strMyPath & "\" & strDBName
'Connect to a data source:
'For pre - MS Access 2007, .mdb files (viz. MS Access 97 up to MS
Access 2003), use the Jet provider: "Microsoft.Jet.OLEDB.4.0". For
Access 2007 (.accdb database) use the ACE Provider:
"Microsoft.ACE.OLEDB.12.0". The ACE Provider can be used for both the
Access .mdb & .accdb files.
connDB.Open ConnectionString:="Provider =
2/06/2014 7:45 PM
17 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
2/06/2014 7:45 PM
18 of 18
http://www.globaliconnect.com/excel/index.php?option=com_content...
< Prev
Next >
Contact
About Us
Testimonials
2/06/2014 7:45 PM