I have a class that’s handling my connection to an Access 2003 database. I would like to setup the same thing only for Access 07/10 .accdb files. Any help is appreciated! Thank you!
Here’s a list of my references and a copy of the class object
References:
- Microsoft Access 14.0 Object Library
- Microsoft DAO 3.6 Object Library
ConnectionClass:
Option Explicit
Private Const DbFile = "\\server\folders\Report.mdb"
Dim OpenConn As DAO.Database
Dim ObjAccess As Object
Private Sub Class_Initialize()
On Error Resume Next
Set OpenConn = DAO.OpenDatabase(DbFile)
If Err.Number = 3024 Then MsgBox "Check connection string in the VBA StaticClass object", vbOKOnly
Set ObjAccess = CreateObject("Access.Application")
ObjAccess.Visible = False
ObjAccess.OpenCurrentDatabase (DbFile)
End Sub
Public Function runSQL(ByVal sql As String) As Recordset
Set runSQL = OpenConn.OpenRecordset(sql)
End Function
Public Function runVolumeReport(ByVal inMacro As String)
ObjAccess.DoCmd.RunMacro inMacro
End Function
Public Function closeResources()
Set ObjAccess = Nothing
OpenConn.Close
End Function
There is an issue in
Class_Initialize.Because of
On Error Resume Next, any error other than 3024 (“Could not find file”) will pass silently and OpenConn will not be set as you intend. Later when you attempt to useOpenConn, you will trigger another error. And, in a comment, you reported you do get another error with this line:Unfortunately, due to
On Error Resume Next, we don’t know whyOpenDatabasefailed leavingOpenConnunset. SinceObjAccessseems to work as an Access application object, you could try settingOpenConntoObjAccess.CurrentDb.OTOH, you may be able to dispense with
OpenConnentirely if you change yourrunSQLfunction like this …