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: –


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: –


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
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.