DAO Information for newbies

H

HK

I'm looking for a good site on DAO for newbies. Anyone have any
recommendations?

I have found some informational sites, but almost always they confuse
the newbie with DAO and ADO information. Also, I haven't seen any with
information on why we need to declare our database etc. For the
longest time I didn't understand that my Access forms are accessing my
data using DAO, even though I never declared anything. I have various
questions like, can I declare my db once at the opening and never have
to set my db variable again? When I reset my recordsource for my form
through code, should I use specific DAO methods to do this? I normally
don't.

Any suggestions on information?
 
R

Roger Carlson

I'd get a copy of "Access 97 Developer's Handbook" by Paul Litwin, Ken
Getz, Mike Gilbert. You can find them used on Amazon. Since Access97 used
only DAO, there's no confusing the two. Another resourse is "DAO Object
Model: The Definitive Guide" by Helen Feddema.

Declaring a database variable as in:
Dim db as DAO.Database
Set db = CurrentDb
is preferred for several reasons.
1) It is good programming practice to explicitly declare and later close and
destroy database objects
2) Every time you called CurrentDb, you are creating a new instance of the
database. This takes extra time and resources. So if you are creating
multiple objects (recordset, querydef, etc) in a routine, you can use the
same database object over again.
3) Some objects cannot be created and used without explicitly creating a
database variable. Some can. But it makes sense to always create objects
in the same way.

RecordsetClone objects are different than normal recordset and Access treats
them differently. That's why you don't create them with a database
declaration.

I generally create and destroy a database instance in every subroutine (that
needs it, of course). Mostly because it's easier to keep track of it that
way. It's very easy to lose track of whether the database object has been
created or destroyed. At most, I will declare one at the module level, but
not often.

I don't understand your question about resetting the recordsource for a
form. What do you normally do?

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
A

Albert D.Kallal

HK said:
I'm looking for a good site on DAO for newbies. Anyone have any
recommendations?

You don't mention what version of ms-access you are using, but in a2003,
while in the code editor (you *must* be in the code editor), simply go

Help-> Microsoft Visual Basic Help

On the right side, you will see a "table of contents", and the 3 one is a
full DAO reference
The 2nd entry is ADO.
Also, I haven't seen any with
information on why we need to declare our database etc.

"why" we do this is rather a general question. Why do we build combo boxes
on a form?

For the most part, I never do declare the database object, and use current
db.

So

dim rstCustomers as dao.recordset


set rstCustomers = currentdb.OpenReocrdSet("select * from tblCustomers where
city = 'N.Y.'")

In the above, it is only two lines of code...and I did not need to, nor care
to declare a database object.
So, you actually don't have to declare a database object, and most of the
time I don't. As you can see,
2 lines code can suffice. And, I could even not declare the reocorset if I
don't want to!!

strCompanyName = currentdb.OpenReocrdSet("select * from tblCustomers where
city = 'N.Y.'")(0)!CompanyName

So, the above one line opens a reocrdsset, and sets the string value of
company to our string var.

So, the "why" we declare a database object is mostly a preference thing on
your part to make using the database
object somewhat easier to work with. However, you don't HAVE to declare the
object, and for the most
part I use currentdb...
For the
longest time I didn't understand that my Access forms are accessing my
data using DAO, even though I never declared anything.

Actually, the above is not always the case. In fact, ms-access is quite
smart,
and if you base a form on a ado reocrdset, then ado is used. If you base a
form on a dao reocordset, then dao is used And, if you use an existing form,
the type of reocreset returned by the form is going to depend on how you
declare your variables.

However, for the most part, you are correct.

I have various
questions like, can I declare my db once at the opening and never have
to set my db variable again?

Sure, you can do the above. You would have to declare a global variable in a
standard code module. Then you are startup code could go

set db = currentdb

However, we all know that developers around the world VERY MUCH will
discourage you to use a global var when you don't need to. Further, what
about importing forms or grabbing code examples from other applications you
built? Perhaps you did not declare that global var, and thus existing code
can't be easily adopted from one application to another. Remember, the
largest cost of development is maintains existing code, and also being able
to re-use code inn other applications. So, the goal making more modular code
is a greater goal then eliminate you having to declare the db object each
time. And, in fact as I mentioned, I never declare it anyway...I use the
built in method called CurrentDb.

Note that if that other code examples and even previous code that you wrote
does not assume a global var called db, the you will have to modify that
code. Further, if you have a un-handled code error, then all global vars are
re-set, and thus you would loose your global db var anyway. So, the benefit
of you predefined a global var are not much, and importing forms code, and
other examples into your existing application will thus have to be modified
to use this global db of yours. There is just not much of a benefit here at
all to use a global db var. However, you *can* use one if you wish....

As mentieond, the key to good software development is to write modular stand
alone code, and that is easer to copy/re-use, debug, and less effort to
maintain in the long run...
When I reset my recordsource for my form
through code, should I use specific DAO methods to do this? I normally
don't.

You don't have to. However, if you have a ADO reference, or use ado code in
your application, then often you should specifically force and declare the
reocrdset type.

So, in place of

me.ReocrdSetClone.FindFirst "id = 1233"

You might conisder suing

dim rstMyClone as dao.RecordSet

set rstMyClone = me.RecordSetClone
rstMyClone.FindFirst "id = 2323"

In the above, since you "force" the type of reocrdset, then there can be no
confusing.

Since I don't use ado at all, then I do NOT HAVE a ado reference set, and
thus I don't have to really worry about this problem...

However, as a habit...I do prefix my dao objects with dao now..

eg
in place of

dim rst as reocrdset

I use

dim rst as dao.recordset
 
H

HK

I sure appreciate the help here. I'm a very inexperienced programmer
and I don't reference the DAO object at all. My code typically looks
like:

My.Recordsource = "Select * from tblNewbie"
Me.Requery

I do like the input I'm getting. Thanks and keep it coming.
 
D

Dirk Goldgar

HK said:
I sure appreciate the help here. I'm a very inexperienced programmer
and I don't reference the DAO object at all. My code typically looks
like:

My.Recordsource = "Select * from tblNewbie"
Me.Requery

I do like the input I'm getting. Thanks and keep it coming.

That code doesn't use DAO *or* ADO at all; at least, not directly.
There, you're manipulating an object that is defined by Microsoft Access
itself: the Form object. (But "My.Recordsource" is likely to be a typo,
and should be "Me.RecordSource".)

Note, by the way, that when you change a form's RecordSource property,
there's no need to requery it. Access automatically requeries the form
when you change its recordsource.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DAO vs ADO 5
Should I Learn DAO, ADO or ADO.NET ? 2
DAO IS DED 18
declare DAO 3
ACC2003/2007 + SQL Server ADO or DAO 10
Determine References Used 17
Is DAO dead (in 2k3 or 2k7)? 12
Access 2007 + SQL Server 7

Top