Late Binding for DAO

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have quite a large project created using DAO and since Access does not
automatically include a reference to DAO, if I distribute this, I know there
will be problems.
Is there a way to do late biding with DAO?? If so, what is the line of
code:
Set rs = CreateObject (????????)

Thanks for any suggestions.
 
Joey said:
I have quite a large project created using DAO and since Access does not
automatically include a reference to DAO, if I distribute this, I know there
will be problems.
Is there a way to do late biding with DAO?? If so, what is the line of
code:
Set rs = CreateObject (????????)

Thanks for any suggestions.


If you create the database with a reference to the DAO library then this
reference should remain. It is only when you create a new database in A2K
onwards that the reference is missing. I have distributed many databases to
many machines and never had a problem when the reference is only to this
library, although I would use late binding for any MS Office automation code
I write.
For DAO, you might do better to write code to check your references rather
than remove them and use late binding. If you really insist, then don't
forget you will also have to define the constants such as dbOpenForwardOnly
when you open a recordset.

Private Sub TestDAO()

On Error GoTo Err_Handler

Dim dbs As Object
Dim rst As Object
Dim strSQL As String

Const dbOpenForwardOnly = 8

Set dbs = Application.CurrentDb

strSQL = "SELECT TOP 5 * FROM tblContacts"

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)

While Not rst.EOF
MsgBox Nz(rst("ConFirst"), "")
rst.MoveNext
Wend

Exit_Handler:

On Error Resume Next

If Not dbs Is Nothing Then
rst.Close
Set rst = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


Note: If the code is from Access you can start from Application.CurrentDb to
do most of things you need - as shown below.
If this is from vbs, where you can't use this, you have to start like:
Set eng = CreateObject("DAO.DBEngine.36")
Set dbs = eng.OpenDatabase("C:\Test\db1.mdb", True)
 
Const dbOpenForwardOnly = 8

what is the number for
dbOpenSnapshot

I can't find it....where does one look for this info?
 
4. The Object Browser is the easiest way to find this stuff. Press F2 in the
VBA editor to display it. It only lists objects from referenced libraries,
so you'll have to add the DAO reference again if you've already removed it,
you can remove it again later.

BTW: I agree with the comments others have made - I've never had a problem
with a DAO 3.6 reference. I'm not saying it can't happen, but it is rare.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Thanks for the tip. I had looked in the Object Browser but couldn't find it
(of cource because I no longer have it refereced....didn't realize it was
only showing referenced libraries)
Appreciate the help!
 
When I try this:
Dim rs as Object
Set rs = CreateObject("DAO.Recordset")

I get runtime error 429: "ActiveX component can't create object"

Do you know why??
Thanks
 
Hmmm.. seems that I erred here, as it appears that this approach does not
work as I'd thought it did.

I suggest that you use the early binding idea where you have DAO library
selected in the References for the developing database. I'm not sure how
long it may take me to see if there is a way to do this at all.... sorry
'bout that!
 
Thanks for clearing that up. I thought it was me....... :-)
If I set a reference to DAO and then move this database to a machine that
only has Access runtime, will it work or error out? Do you know?
Thanks
 
If the runtime was created from a database that had the DAO reference set,
it should be ok.
--

Ken Snell
<MS ACCESS MVP>
 
I think it should be OK - I believe Access itself uses DAO 'behind the
scenes', so if the Access runtime is installed, I expect DAO is also
installed. However, I believe you could still use late binding if you wanted
to, by using CurrentDb which, in an MDB, returns a DAO Database object
regardless of whether you have a reference to the DAO object library or not.
Something like ...

Dim db As Object
Dim rs As Object

Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM SomeTable")

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Back
Top