Results 1 to 3 of 3

Thread: Determine if instance of access is open

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    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:
    1. Sub Main
    2.  
    3.     Dim acc As New Access.Application
    4.     acc.OpenCurrentDatabase
    5.  
    6. 'How do I determing if acc is open?  In the example above I
    7. 'know its open, but there are cases in my program when I don't know if its been opened or not.
    8. 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

  2. #2
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Friend moApp As Access.Application
    2. Private mbKillMe As Boolean
    3.  
    4. Friend Property KillMe() As Boolean
    5.     Get
    6.         KillMe = mbKillMe
    7.     End Get
    8.     Set(ByVal Value As Boolean)
    9.         mbKillMe = Value
    10.     End Set
    11. End Property
    12.  
    13. Friend Sub InitializeMe()
    14.     Try
    15.         '<INITIALIZE ACCESS>
    16.         moApp = DirectCast(GetObject(, "Access.Application"), Access.Application)
    17.     Catch ex As Exception
    18.         'Access not running. Create a new instance or not. Up to you.
    19.         If TypeName(moApp) = "Nothing" Then
    20.             moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
    21.             mbKillMe = True
    22.         Else
    23.             MessageBox.Show(ex.Message, "VB/Office Guru™", _
    24.             MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    25.         End If
    26.     End Try
    27. End Sub
    28.  
    29. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    30.     '<CALL THE SPELLME INITIALIZATION PROCEDURE BEFORE ANY USE>
    31.     InitializeMe()
    32. End Sub
    33.  
    34. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    35.     If KillMe = True Then
    36.         moApp.Quit(False)
    37.     End If
    38.     moApp = Nothing
    39. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width