AccessBlog.net

News, links, downloads, tips and tricks on Microsoft Access and related

About Me Search
Alex
Name:Alex Dybenko

Location:Moscow, Russia

Friday, January 27, 2012

How to get most recent DBEngine object

Below is Graham Mandeno's version of  VBA GetDBEngine(), now in VB.NET. If user have several versions of DAO installed this function returns reference to latest available.

 1:  Public Function GetDBEngine() As Object
 2:  Dim aVersions() As String = {"120", "36", "35"}
 3:  Dim i As Integer
 4:  GetDBEngine = Nothing
 5:  Try
 6:   For i = LBound(aVersions) To UBound(aVersions)
 7:    Try
 8:      GetDBEngine = CreateObject("DAO.DBEngine." & _
        aVersions(i))
 9:      Exit For
 10:   Catch ex As Exception When Err.Number = 429
 11:   'continue after "Cannot create ActiveX component"
 12:   Catch
 13:    Throw
 14:   End Try
 15:   If i = UBound(aVersions) Then _ 
        Throw New System.Exception("No known version" & _         " of DAO is available")
 16:   Next
 17:  Catch ex As Exception
 18:   Throw New System.Exception("Cannot create " & _
      "DBEngine" & vbCrLf & ex.Message)
 19:  End Try
 20:  End Function

Labels: , ,

0 Comments:

Post a Comment

<< Home