DAO

  • Thread starter Thread starter lmcc007
  • Start date Start date
L

lmcc007

I am trying to understand some code I keep seeing, for example:

Dim dbMyDB As DAO.Database
Set dbMyDB = DBEngine.Workspace(0).Databases(0)

I found explanations for DAO and workspace, but it does not make sense to
me. I need non-technical explanations or explanations explained simple
enough that a dummy can understand it. For some reason I can't comprehend why
DAO and workspace is needed or what it is used for in Access.

Any simple explanations and/or examples are appreciated.

Thanks!
 
hi,
I am trying to understand some code I keep seeing, for example:

Dim dbMyDB As DAO.Database
Set dbMyDB = DBEngine.Workspace(0).Databases(0)

Any simple explanations and/or examples are appreciated.
The first line declares an object variable for holding a database instance.
The second line assigns a database instance (the current database) to
this object variable.


mfG
--> stefan <--
 
DBEngine.Workspace(0).Databases(0) can be seen as the equivalent of a call
to CurrentDB but without some internal initialisation performed by
CurrentDB; so the performance is better.

As to explain why your code need a call to
DBEngine.Workspace(0).Databases(0) (or to CurrentDB), it's impossible to
tell you without seeing what kind of operation is performed after that with
the newly instanciated dbMyDB object. However, by searching the internet
for CurrentDB, probably that you'll find all the necessary information.

--
Sylvain Lafontaine, ing.
MVP - Windows Live Platform
Email: sylvain2009 sylvainlafontaine com (fill the blanks, no spam please)
Independent consultant and remote programming for Access and SQL-Server
(French)
 
And to explain the concept of an Object...

You may have heard the term Object-Oriented programming. In order to
understand the concept, think of an object as a person. Every person has
certain traits - such as skin color, hair color, eye color, weight, age,
height. Every person also can also take any one of a number of actions -
stand up, sit down, pick up item, set item down. Along with this, some
people can take actions that others can - drive car, fly a plane. There are
also situations where a person has multiple items of the same thing, for
example one person might speak a single language, where as another person
might speak multiple languages.

Object-oriented programming takes the concept of traits and actions and
applies them to programming, only instead of dealing with people we're
dealing with programs and different types of items that the programs work
with. For the sake of simplicity, everything is referred to conceptually as
an Object - wether it is a program or an item that the program works with.

By item, I'm referring to any number of things that the program works with.
It could be a table in an Access database. It could be a Form to display and
manipulate records. It might be a mail message in Outlook. It could be cell
in an Excel spreadsheet. It could be a slide in a Power-point presentation.

Whereas people have traits and can take various actions, in programming
these are referred to as properties and methods. Hair color would be a
property. Fly a plane would be a method.

The properties and methods for any given object vary widely depending on the
type of object that you're working with and won't neccessarily be same from
one object to another. For example, while a human might have a property name
'language', a cat would not. Likewise, a cat might have a property named
'Tail Length' where as a human obviously would not.

Nearly all object-oriented programming languages include some form of Object
Model which is documentation that spells out the various objects and their
properties and methods. If you go into Access Help and search Access Object
Model, you'll see that there's a diagram that shows the various objects and
their relationships to each other.

Object variable is a special type of variable that holds an object, as
opposed to a variable that holds a value such as 'David Smith'. The object
variable has to be instantiated which is simply telling the language (in
this case Access VBA) that this object variable is a specific item. In the
example, below the programmer is instructing Access to create an object
variable that points to the current database. Once created, the developer
can work with the properties and methods of the current database as in...

Debug.Print dbMyDB.name 'Tells Access to get the Name property of the
object and show the value in the Immediate Window
dbMyDB.Close 'Tells Access to execute the CLOSE
method of the object, essentially closing the database

Some properties are Read Only , pretty much all of the properties of the
Database are. But if you're working with an Object that represents a Control
on a Form as in ...

Dim ctl as Control
Set ctl = [Forms]![FormName]![ControlName]

You have access to properties that you can set for example...

ctl.Value = "Test" 'Sets the value of
the control to Test
ctl.ControlSource = "[txtCity]" 'Sets teh control source to
a field named "txtCity"
ctl.FontWeight = "Bold" 'Sets the font weight to
Bold (sample only, the actual syntax is slightly different)

This of course assumes that the Control is a TextBox. If the control is a
Label, the first two statements will create an error as those properties do
not exist for a Label.
 
I am trying to understand some code I keep seeing, for example:

Dim dbMyDB As DAO.Database
Set dbMyDB = DBEngine.Workspace(0).Databases(0)

I found explanations for DAO and workspace, but it does not make sense to
me. I need non-technical explanations or explanations explained simple
enough that a dummy can understand it. For some reason I can't comprehend why
DAO and workspace is needed or what it is used for in Access.

Any simple explanations and/or examples are appreciated.

Thanks!

Well... it's a complex technical subject, and you're asking for an answer
without complexity or technical issues.

A brief, nontechnical, and therefore incomplete and inadequate answer is that
DAO is the name Microsoft gave to the program library which manages JET
database objects. JET is (or was, 2007 has something newer) the default
"database engine" for Access databases; a "Workspace" is a software "place" to
keep track of databases (usually only one but you can open several); and DAO
is a library of programs which let you work with databases and the objects in
databases.

If you don't have the DAO program library, then you'll have a whole bunch of
Tables, Recordsets, Queries, etc. etc. and no programmatic way to do anything
with them. It's like the engine, transmission, gearbox, differential and axle
of your car - it's all the "under the hood" stuff that makes the car work.
 
JET is (or was, 2007 has something newer)

The ACE is just Jet 5 (or 4.5) with a new name, one that tries to
avoid the old problem of distinguishing Access and Jet. But it
actually makes it worse by acquiescing to the tendency to refer to
Jet/ACE data files as Access databases.
 
use of Workspaces.

One reason I use workspace is to start a transaction on 'some' of the
tables, but not on others. As example, let say you get a list of airplanes
landing and takeoff. To VALIDATE the list, someone ask to check that a
takeoff follow a landing (ie, no two consecutive takeoff for the same plane,
no two consecutive landing either). So, I start a transaction on the
movements table, append the data and, one record at a time, check if all the
applicable rules are satisfied. If they are, ok, nice, I 'commit' the
transaction. But if some error occur, I rollback the transaction up to its
start, so I am left with the system as it was before I started the process.
The problem is that the end user will then ask: what was THE error (or the
errorS )? If I have only ONE workspace, I would not write the error in the
movementsErrors table, since that table too would be rolled back to its
initial state, in case of an error, and thus, I won't be able to read
whaterver I wrote to it, while I was processing the data, in the
transaction. So, I create TWO workspaces, one that will be in the
transaction (and eventually rolled back) and one which will NOT be in the
transaction, and thus, unaffected by the possible rollback of the
transaction, and keeping the wanted error log I generated while analysing
the imported data.


use of Databases.

I don't use more than one database object at a time, but I imagine that
someone may find some uses to open the same database multiple times (such as
when using replication), somehow allowing some work while the replication is
preformed in parallel ( is it a good idea? ) without having to open TWO
Access.exe ( ? ). It could also be used to Connect( ) to a 'secure'
database, sending supplied password. But again, I have never had to do it.



DBEngine(0)(0)

which stands for DBEngine.Workspaces(0).Databases(0) is simply the plain
database object that Access made for you, by default.



Vanderghast, Access MVP
 
It seemed as if the original poster had never been exposed to the underlying
concepts. One of my pet peeves with learning ASP.NET over the past several
months is people providing the technically correct answer which fails to
provide any overal insight. Such as 'just create a user control that
inherits the web.ui.control class and override the base method, just be
certain to call the original method' without any elaboration. Of course,
that's one of the more specific answers.

Besides, it not as if I brought up Classes in my resonse.
 
Back
Top