Reputation: 2787
The following code to close the Access Navigation Pane works.
DoCmd.NavigateTo "acNavigationCategoryObjectType"
DoCmd.RunCommand acCmdWindowHide
But the problem is that if the Navigation Pane is already closed then the acCmdWindowHide will just hide any other object (i.e. form, table) which was open. I use DoCmd.TransferDatabase in my code and when this is executed the Navigation Pane is sometimes opened. This may happen if a warning message about data import appears and the user clicks cancel. To be sure the user does not see the pane I want to hide it but if it is already hidden then there is nothing to hide but the above command just hides my form and that is not what I want.
The line
DoCmd.NavigateTo "acNavigationCategoryObjectType"
is always executed and does not return anything. It does not return an error if there is no Navigation Pane to navigate to.
My question is: How can I determine if the Navigation Pane is currently open so that I know I have to close it. Or how can I make sure that I close the Navigation Pane but no other object if I use the above code?
Upvotes: 1
Views: 3345
Reputation: 1295
How can I determine if the Navigation Pane is currently open so that I know I have to close it. Or how can I make sure that I close the Navigation Pane but no other object if I use the above code?
By holding the Application.CurrentObjectName
value before setting focus to the Nav Pane,
and then comparing that value to Application.CurrentObjectName
after DoCmd.NavigateTo ("acNavigationCategoryObjectType")
, you can tell if the Nav Pane can be closed.
I found that the code suggested in other examples would fail if a search filter was currently set in the navigation pane. In some cases this would cause the active form to close. That can lead to a bad user experience. This routine should get around that. I have only found two limitations:
In either of those cases the sub will not hide the navigation pane which is better than close the active form.
Public Sub HideNavPane()
' This will hide the Navigation Pane.
' It works even if a search filter is set, unlike other solutions that may
' inadvertently close the active object.
' Limitations: An object (form, report, query, etc) must be open and it
' cannot be the same name as the selected item in the nav pane
' or this will do nothing.
Dim strCurrentObjectName As String
strCurrentObjectName = Application.CurrentObjectName
' Move focus to the navigation pane/database container
DoCmd.NavigateTo ("acNavigationCategoryObjectType")
If strCurrentObjectName <> Application.CurrentObjectName Then
' The Navigation Pane is open and has focus.
' Use the window menu to hide the navigation pane
DoCmd.RunCommand acCmdWindowHide
End If
End Sub
Upvotes: 2
Reputation: 27644
This forum thread suggests:
Public Function HideIt()
' Employee is just any existing table
DoCmd.SelectObject acTable, "Employee", True
If Application.CurrentObjectName = "Employee" Then DoCmd.RunCommand acCmdWindowHide
End Function
Upvotes: 1