How To… Referencing The DAO Engine

In VB Script (or sometimes in general application development and office application development where late binding is needed), it is necessary to create a DAO engine object. Either to access database through DAO, or given there are better ways to access the data more likely to access properties within an Access Database file, and manage tables or hidden file properties for a legacy system.
 
Traditionally you may have used something like: –

Dim

DBEngine As Object
Set DBEngine = CreateObject("DAO.DBEngine")

Which you would expect to get the latest installed/registered version of the engine. This however nolonger works, so we must be a little more indepth and use a function to get our object. Such as: –

Public

Function DAO_DBEngine() As Object

On Error Resume Next

‘try Access 2007 (DAO Engine version 120)
Set GetDBEngine = CreateObject("DAO.DBEngine.120")

If Err.Number <> 0 Then

‘try DAO version 3.6
Err.Clear
Set GetDBEngine = CreateObject("DAO.DBEngine.36")

If Err.Number <> 0 Then

Set GetDBEngine = CreateObject("DAO.DBEngine.35")

End If

End If

End Function

If the function returns nothing the object has not been created, otherwise it will return the latest available version of DAO, and allow the querying of the database engine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s