Link to home
Start Free TrialLog in
Avatar of upsfa
upsfa

asked on

Programmatically sort the objects in the Navigation Pane in Access 2010

In Microsoft Access 2010, I would like to be able to programmatically sort the objects in the Navigation Pane, but I have not been able to find the correct syntax to use.  

To manually sort, the user needs to right click the Navigation Pane header and select select Sort By, then select the field they want to sort their objects by.  But if you want to sort descending, the user needs to right-click again, select Sort By,  and then select Descending.  I want to create a macro, or use some VBA that I can associate with a button, so I can sort descending by last modified date with one click, rather than two right-clicks and four menu selections.  I do this sort all day, always looking for the most recent queries I have been working on.
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Not sure if I have ever seen this done (even in Access 2003 and older)

So you are giving your users complete and direct access to all Database objects instead of controlling this through forms...?


You can certainly create your own sort form quite easily.

Take the form from here and drop it into your database
(I am sure you will be able to easliy adapt it for different sort field options)
Database41.mdb
Avatar of upsfa
upsfa

ASKER

Thanks, not what I'm looking for.   I already have a couple of forms that replicate the old database container.  Here's a good one.
SortByDesc.zip
Again, I am not aware of a direct way that this can be done via VBA.
But perhaps thee is, so let's see what other experts post

And again, is this just for you as the developer?
Most Access Applications are designed so that direct access to the database window/ Task Pane is not needed.

Avatar of upsfa

ASKER

This is not for end-users, just me a couple of my people.  Actually, I would put this in my blank.accdb template so it is available in any database I create.

Here's where I am going now.  To get to the sort options manually, you can click on the Navigation Pane header, and invoke the context menu by right-clicking.  Alternatively, if the header has the focus, you can use shift-F10 to invoke the menu.  So, I’m thinking sendkeys, although I try to avoid this.  In a macro, I have used the Navigate To function to take me to Tables, then I use shift-tab, four times to move the focus to the header, then use shift-f10, but it does not work.  When I run the macro up to the forth shift-tab line, then manually press shift-F10, it works.  If I manually place the focus on the Navigation Pane header, then run a simple macro that only does the shift-F10 sendkeys command that works, but when I put them together, they it does not work.

Ok, while I was typing that it gave me an idea – create one macro that gives the header the focus, then call another macro that does the shift-F10 part.  It works!

The three macros are attached
Option Compare Database

'------------------------------------------------------------
' SortObjects - This will be run first, it will navigate to the tables section
' of the navigation pane, then will tab up to the navigation pane header, then
' will call the macro that will excute the shift-F10 command to invoke the context menu
'------------------------------------------------------------
Function SortObjects()
On Error GoTo SortObjects_Err

    DoCmd.NavigateTo "acNavigationCategoryObjectType", "acNavigationGroupTables"
    SendKeys "+{Tab}", True
    SendKeys "+{Tab}", True
    SendKeys "+{Tab}", True
    SendKeys "+{Tab}", True
    DoCmd.RunMacro "SortByModDescending", , ""

SortObjects_Exit:
    Exit Function

SortObjects_Err:
    MsgBox Error$
    Resume SortObjects_Exit
End Function
Option Compare Database

'------------------------------------------------------------
' SortByModDescending - This opens the context menu, then selects sort by last modified date
' then opens the menu again and selects Descending, then NavigateTo is used to send the
' focus to the queries section of the navigation pane, then sendkeys is used to give the
' header focus again.  The ShowAllGroups macro is then called to show all object types.
'
'------------------------------------------------------------
Function SortByModDescending()
On Error GoTo SortByModDescending_Err

    SendKeys "+{f10}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{right}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{enter}", False
    SendKeys "+{F10}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{right}", False
    SendKeys "{down}", False
    SendKeys "{enter}", False
    DoCmd.NavigateTo "acNavigationCategoryObjectType", "acNavigationGroupQueries"
    SendKeys "+{tab}", False
    SendKeys "+{tab}", False
    SendKeys "+{tab}", False
    DoCmd.RunMacro "ShowAllGroups", , ""


SortByModDescending_Exit:
    Exit Function

SortByModDescending_Err:
    MsgBox Error$
    Resume SortByModDescending_Exit

End Function


Option Compare Database

'------------------------------------------------------------
' ShowAllGroups - This invokes the context menu and selects Show All Groups
'
'------------------------------------------------------------
Function ShowAllGroups()
On Error GoTo ShowAllGroups_Err

    SendKeys "+{f10}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{down}", False
    SendKeys "{enter}", False

ShowAllGroups_Exit:
    Exit Function

ShowAllGroups_Err:
    MsgBox Error$
    Resume ShowAllGroups_Exit

End Function

Open in new window

OK
Great,
If this is what works, then you can accept your own post as the solution.


Just a note here.
MS is always threatening to Deprecate SendKeys.
So be prepared for the day when this becomes a reality...

Jeff
Avatar of upsfa

ASKER

It only works when I select the macro manually.  If I add a button to the QAT and use that to run the macro the header does not get the focus (regardeless of how many times I shift-tab).  Really want this to work from the QAT
<Really want this to work from the QAT>

Then you may have an uphill battle here.

Again, if it were me, I would just create a standalone form that does this...
ASKER CERTIFIED SOLUTION
Avatar of upsfa
upsfa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of upsfa

ASKER

"No body cares more about your problems then you"
Congratulations!

Just keep in mind my comments about MS continual threatening to deprecating Send keys...
You may find issues with SendKeys in Win 7 ...

mx