database automation using vba (advanced ......database automation using vba (advanced microsoft...

31
DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6) Database Program: Microsoft Access Series Instructor: Michael Kremer, Ph.D. Technology & Information Management Section 7

Upload: others

Post on 13-Mar-2020

53 views

Category:

Documents


2 download

TRANSCRIPT

DATABASE AUTOMATION USING VBA

(ADVANCED MICROSOFT ACCESS, X405.6)

Database Program: Microsoft Access Series

Instructor: Michael Kremer, Ph.D.Technology & Information Management

Section 7

AGENDA

11. Introduction to Data Access Technologies

12. Data Access Objects (DAO)

Introduction to Data Access Technologies

11.

MS Access supports two data access technologies:

Data Access Objects (DAO)

ActiveX Data Objects (ADO)

DAO is tailored towards Jet/ACE database engine, ADO more

universal model to access any data.

Updating data in Access:

Form Object

Action Query

Docmd.RunSQL

But sometimes more complex SQL programming is needed.

11.1 OVERVIEW OF DATA ACCESS TECHNOLOGIES

208

11.1 OVERVIEW OF DATA ACCESS TECHNOLOGIES

209

Docmd.RunSQL method is a powerful way to process Action

queries in VBA.

Create SQL statement as a string, and include dynamic Where

clause, for example.

Docmd.RunSQL SQLstring

11.2 SQL PROGRAMMING USING VBA

210

Default Access warning messages are displayed when running Action queries, even in VBA.

Use Docmd.SetWarningsmethod to turn off messages.

11.2 SQL PROGRAMMING USING VBA

211

11.2 SQL PROGRAMMING USING VBA

212

SQL statement is passed as a string.

Important to understand how to build correct SQL strings, especially when using quotes within quotes.

SQL needs quotes for textual criteria. Date values need pound (#) symbols, and numbersdo not need any delimiters.

When using single quotes, it may pose problems if data contains

apostrophes. Therefore, use chr function.

11.2 SQL PROGRAMMING USING VBA

213

11.2 SQL PROGRAMMING USING VBA

214

Data Access Objects (DAO)

12.

DAO is the programmatic interface between VBA and Access

database engine, ODBC and ISAM.

DAO 12.0 latest version, supports latest ACCDB format.

DAO is the preferred method since it connects natively to the

Access database engine.

Linked tables (even Oracle/SQL server) can be manipulated using

DAO.

The object hierarchy starts out with the DBEngine object

(comparable to Application object).

12.1 OVERVIEW OF DAO

215

Collection of Workspaces managesdifferent transactions.

Workspace is a non-persistent object(meaning it exists only in memory,cannot be saved to disk).

Even if no workspace is created, a default workspace exists when you open Access.

Connection object is only needed for non-Access database, such as ODBC databases.

Databases collection manages all database connections in memory, you can connect to more than one database using DAO.

Users/Groups is for Access security

12.2 DAO OBJECTS

216

A database object has a collection of tables,

the TableDefs collection.

Containers collection contains all saved objects

in an Access database.

QueryDefs collection contains all queries in

current database.

Recordsets are data

pointers in memory

based on tables,

queries, or SQL.

12.2 DAO OBJECTS

217

To get started with DAO programming, you need to point to a

database object.

From the DBEngine, you go down through the Workspace object to

a database object:

Dim wks as DAO.Workspace

Dim db as DAO.Database

Set wks = DBEngine.Workspaces(0)

Set db = wks.Databases(0)

The 0 index points to the current Workspace object within the

Workspaces collection (default)

The 0 index points to the current database in the Databases

collection.

12.3 DAO DATABASE OBJECTS

218

If no workspace reference is needed,

then point to the current database:

Set db = DBEngine.Workspaces(0).Databases(0)

Access contains function to point to current db:

Set db = CurrentDb()

To create a reference to another database:

Set db = [workspace].OpenDatabase(“path/file”)

12.3 DAO DATABASE OBJECTS

219

Workspace is a named session for a user.

All objects opened in one workspace share one transaction scope.

Create additional workspaces to conduct multiple, simultaneous , and overlapping transactions.

wks.BeginTrans: All db transaction after this statement are deferred in a separate memory location.

wks.CommitTrans: End current transaction and save to the database.

wks.Rollback: End current transaction and not save to the database.

12.3 DAO DATABASE OBJECTS

220

12.3 DAO DATABASE OBJECTS

221

Recordset is a pointer to a virtual dataset in memory.

Can be entire table, subset of a table, query, or SQL statement.

After creating recordset object, use VBA to programmatically

access the data.

Dim rs As DAO.RecordsetSet rsvariable = CurrentDb.OpenRecordset(Name [,Type [,Options [, Lockedit ] ] ])

Type refers to: Table, Dynaset, Snapshot

Table type is local table in current database (not even a linked

table).

Dynaset type is linked table, query, or SQL statement

12.4 DAO RECORDSET OBJECTS

222

Snapshot type is read only, uses less resources but it is not

updatable.

When recordsets are prepared for updating, data in the database

is locked.

Default type for a table is dbOpenTable.

Default type for a query is dbOpenDynaset.

To remove a recordset object from memory, close the variable:

rs.Close

To refer to a field within a recordset, access the fields collection

and then point to a specific field.

rsvariable.Fields!fieldname.Value

12.4 DAO RECORDSET OBJECTS

223

12.4 DAO RECORDSET OBJECTS

224

12.4 DAO RECORDSET OBJECTS

225

12.4 DAO RECORDSET OBJECTS

226

12.4 DAO RECORDSET OBJECTS

227

Sorting/Filtering can be performed on recordset objects, however, it is easier and better for performance if you already sort and filter when you create the recordset object.

For table-type recordset, you can use existing index.

12.5 SORTING/FILTERING OF RECORDSETS

228

To sort a recordset object, you have to use the Sort method.

Then you have to create a second recordset based on the first one and at that time it will apply the sort order. Fairly cumbersome

process.

12.5 SORTING/FILTERING OF RECORDSETS

229

Move method of recordset object to navigate through the records.

recordset.MoveFirst moves the pointer to the first record

recordset.MoveLast moves the pointer to the last record

recordset.MoveNext moves the pointer to the next record

recordset.MovePrevious moves the pointer to the previous record

Good idea to issue a MoveFirst or MoveLast command after creating a recordset object to make sure to point to a current record.

Run-time error if movedtoo far

12.6 LOOPING THROUGH RECORDSETS

230

Test the recordset limits by using EOF (end of file) or BOF (beginning of file).

BOF returns true if before the first record or if no record exists at all in the recordset object.

EOF returns true if after the last record or if no record exists at all in the recordset object.

If BOF and EOF return true, then recordset is empty.

Loop Syntax:

Make sure to issue a MoveLast or MoveFirst to avoid run-time errors.

12.6 LOOPING THROUGH RECORDSETS

231

12.6 LOOPING THROUGH RECORDSETS

232

12.6 LOOPING THROUGH RECORDSETS

233

Recordcount property behaves differently depending on type of recordset:

For table-type, number of records in recordset

For dynaset and snapshot, number of records accessed.

Therefore, move to last record, then use a recordcount.

12.6 LOOPING THROUGH RECORDSETS

234