Module: Objects in Category Microsoft Access : Objects from Total Visual SourceBook

Manage open Access objects, inventory, active objects, hide and unhide, and minimizing, restoring and closing them in Access VBA.

Supports the current Jet database or Access Data Project (ADP).

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modObjects module.
ObjectExists Procedure Determine if this object name exists in the current project.
ObjectNamesToArray Procedure Loads an array with names of all of an object type.
IsObjectHidden Procedure Determine if the specified Access object type and name is hidden.
UnhideObject Procedure Unhide an Access object if it's hidden. For hiding and unhiding all objects in your database, use the HideAllAccessObjects in module modApplication.
OpenObjectsToArray Procedure Loads an array with names of all open objects of an object type.
CloseAllOpenForms Procedure Close all the currently opened forms.
CloseAllOpenReports Procedure Close all the currently opened reports.
CloseAllOpenFormsExceptOne Procedure Close all the currently opened forms except for a specified form.
CloseAllOpenReportsExceptOne Procedure Close all the currently opened forms except for a specified report.
CloseAllOpenObjects Procedure Close all objects open through the user interface.
CloseObjectsOfType Procedure Close all Access objects of the supplied type.
GetActiveDatasheet Procedure Get the name of the currently active datasheet.
GetCurrentObject Procedure Get the name of the current object. Note that if you select objects in the Database Window, this procedure returns the object you selected in that window (i.e. the object does not actually need to be open).
IsObjectOpen Procedure Determine if the named Access object is open.
MaximizeObject Procedure Maximizes the specified object.
MinimizeObject Procedure Minimizes the specified object.
RestoreObject Procedure Restores the specified object (the Windows Restore command).
' Example of modObjects
'
' To try this example, do the following:
' 1. Create a new form in your copy of Sample.mdb (in the Total Visual SourceBook sample directory).
' 2. Create the following command buttons:
'         cmdTest
'         cmdObjectExists
'         cmdCloseTables
'         cmdCloseReports
'         cmdCloseForms
'         cmdCloseAll
' 3. Paste this code into the form's module

' This example assumes that you have the following objects in the current database:
Private Const mcstrTable As String = "Categories"
Private Const mcstrReport As String = "rptInvoice"

Private Sub cmdCloseAll_Click()
  ' Example of CloseAllOpenObjects
  If MsgBox("Do you want to close all open objects, including the current form?", vbYesNo) = vbYes Then
    Debug.Print "Closing all open objects."
    CloseAllOpenObjects acSavePrompt
  End If
End Sub

Private Sub cmdCloseForms_Click()
  ' Example of CloseAllOpenFormsExceptOne
  If MsgBox("Do you want to close all forms except the current form?", vbYesNo) = vbYes Then
    Debug.Print "Closing all forms except the current one."
    CloseAllOpenFormsExceptOne Me.name, acSaveYes
  End If

  ' Example of CloseAllOpenForms
  If MsgBox("Do you want to close all forms including the current form?", vbYesNo) = vbYes Then
    Debug.Print "Closing all forms."
    CloseAllOpenForms (acSavePrompt)
  End If
End Sub

Private Sub cmdCloseReports_Click()

  DoCmd.OpenReport mcstrReport, acViewDesign

  ' Example of CloseAllOpenReportsExceptOne
  If MsgBox("Do you want to close all reports except " & mcstrReport & "?", vbYesNo) = vbYes Then
    Debug.Print "Closing all reports except " & mcstrReport
    CloseAllOpenReportsExceptOne mcstrReport, acSavePrompt
  End If

  ' Example of CloseAllOpenReports
  If MsgBox("Do you want to close all reports?", vbYesNo) = vbYes Then
    Debug.Print "Closing all reports."
    CloseAllOpenReports acSavePrompt
  End If
End Sub

Private Sub cmdCloseTables_Click()
  ' Example of closing all open tables with CloseObjectsOfType
  If MsgBox("Do you want to close all tables?", vbYesNo) = vbYes Then
    Debug.Print "Closing all tables."
    CloseObjectsOfType acTable, acSavePrompt
  End If
End Sub

Private Sub cmdObjectExists_Click()
  ' Does a table name exist in the current project?

  Dim strName As String
  Dim strMsg As String
  Dim fIsHidden As Boolean

  strName = InputBox("Enter a table name to test", , "MsysObjects")
  If strName <> "" Then
    If ObjectExists(acTable, strName) Then
      fIsHidden = IsObjectHidden(acTable, strName)
      strMsg = " exists and is" & IIf(fIsHidden, "", " not") & " hidden"
    Else
      strMsg = " does not exist"
    End If
    MsgBox "Table " & strName & strMsg
  End If
End Sub

Private Sub cmdTest_Click()
  Dim strResult As String
  Dim astrObjs() As String
  Dim intCount As Integer
  Dim intI As Integer

  ' Example of ObjectNamesToArray
  intCount = ObjectNamesToArray(acForm, astrObjs)
  Debug.Print intCount & " forms in this database: "
  For intI = 0 To intCount - 1
    Debug.Print astrObjs(intI)
  Next intI

  ' Example of OpenObjectsToArray
  intCount = OpenObjectsToArray(acForm, astrObjs)
  Debug.Print intCount & " form(s) currently open: "
  For intI = 0 To intCount - 1
    Debug.Print astrObjs(intI)
  Next intI

  ' Example of GetActiveDatasheet
  DoCmd.OpenTable mcstrTable
  strResult = GetActiveDatasheet()
  Debug.Print "Active Sheet: " & strResult

  ' Example of GetCurrentObject
  strResult = GetCurrentObject(acTable)
  Debug.Print "Current table: " & strResult

  ' Example of IsObectOpeon
  If IsObjectOpen(acTable, mcstrTable) Then
    Debug.Print "Categories table is open"
  Else
    Debug.Print "Categories table is not open"
  End If

  ' Example of MaximizeObject
  Debug.Print "Maximizing object " & mcstrTable
  MaximizeObject acTable, mcstrTable

  ' Example of MinimizeObject
  Debug.Print "Minimizing object " & mcstrTable
  MinimizeObject acTable, mcstrTable

  ' Example of RestoreObject
  Debug.Print "Restoring object " & mcstrTable
  RestoreObject acTable, mcstrTable

End Sub

Private Sub Form_Open(Cancel As Integer)
  With cmdTest
    .Caption = "Test Access object functions"
    .Top = 100
    .Width = 5000
    .Height = 400
    .Left = 100
  End With
  With cmdObjectExists
    .Caption = "Object Exists"
    .Top = 600
    .Width = 5000
    .Height = 400
    .Left = 100
  End With
  With cmdCloseTables
    .Caption = "Close tables"
    .Top = 1100
    .Width = 5000
    .Height = 400
    .Left = 100
  End With
  With cmdCloseReports
    .Caption = "Close reports"
    .Top = 1600
    .Width = 5000
    .Height = 400
    .Left = 100
  End With
  With cmdCloseForms
    .Caption = "Close forms"
    .Top = 2100
    .Width = 5000
    .Height = 400
    .Left = 100
  End With
  With cmdCloseAll
    .Caption = "Close all objects"
    .Top = 2600
    .Width = 5000
    .Height = 400
    .Left = 100
  End With
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS