Determine if instance of access is open
Hi, all is there a way to determine if my access object is open? (meaning the database is open via my object) Here is my code:
VB Code:
Sub Main
Dim acc As New Access.Application
acc.OpenCurrentDatabase
'How do I determing if acc is open? In the example above I
'know its open, but there are cases in my program when I don't know if its been opened or not.
End Sub
I was thinking there should be something similiar to the ADO connection object (cn.state) but it wasn't. And (if acc is Nothing) doesn't determine if its open.
Thanks,
Strick
Re: Determine if instance of access is open
I would recommend just setting a boolean property called IsOpen. Set it to True when you open Access and False when you close it.
Are you writing in Access or VB.NET? If VB.NET why use the Access.Application? Also, instantiating Access like that can come back to bite you if you have different versions of Access around.
Re: Determine if instance of access is open
This is what I use for Access to attach to a running instance. Then f its not running I can create a new instance.
VB Code:
Friend moApp As Access.Application
Private mbKillMe As Boolean
Friend Property KillMe() As Boolean
Get
KillMe = mbKillMe
End Get
Set(ByVal Value As Boolean)
mbKillMe = Value
End Set
End Property
Friend Sub InitializeMe()
Try
'<INITIALIZE ACCESS>
moApp = DirectCast(GetObject(, "Access.Application"), Access.Application)
Catch ex As Exception
'Access not running. Create a new instance or not. Up to you.
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
mbKillMe = True
Else
MessageBox.Show(ex.Message, "VB/Office Guruâ„¢", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'<CALL THE SPELLME INITIALIZATION PROCEDURE BEFORE ANY USE>
InitializeMe()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If KillMe = True Then
moApp.Quit(False)
End If
moApp = Nothing
End Sub