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.
LVL 1
upsfaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
upsfaConnect With a Mentor Author Commented:
Victory is mine!

Option Compare Database

'------------------------------------------------------------
' This will sort objects in the database window descending
' by Modified date.
'
' Add this into a module in Access, then
' add an icon for this macro to the Quick Access Tool Bar
' and you will be able to perform the sort with one click so
' you can sort descending by last modified date with one click,
' rather than two right-clicks and four menu selections.
'
'  *  This will not work if your cursor is in a field on
'     a table or query, since CTRL F will then bring up the
'     Find dialog box. Just click on any object in the
'     Navigation Pane, before invoking this code.
'------------------------------------------------------------

Function SortObjects()
On Error GoTo SortObjects_Err

    SendKeys "^f", False
    SendKeys "+{tab}", False
    SendKeys "+{tab}", False
    SendKeys "+{f10}", False
    SendKeys "s", False
    SendKeys "m", False
    SendKeys "{enter}", False
    SendKeys "s", False
    SendKeys "c", False
    SendKeys "{enter}", True
    SendKeys "^f", True
    SendKeys "+{tab}", False
    SendKeys "+{tab}", False
    SendKeys "+{f10}", False
    SendKeys "a", False


SortObjects_Exit:
    Exit Function

SortObjects_Err:
    MsgBox Error$
    Resume SortObjects_Exit

End Function

Open in new window

0
 
Jeffrey CoachmanMIS LiasonCommented:
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
0
 
upsfaAuthor Commented:
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
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
Jeffrey CoachmanMIS LiasonCommented:
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.

0
 
upsfaAuthor Commented:
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

0
 
Jeffrey CoachmanMIS LiasonCommented:
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
0
 
upsfaAuthor Commented:
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
0
 
Jeffrey CoachmanMIS LiasonCommented:
<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...
0
 
upsfaAuthor Commented:
"No body cares more about your problems then you"
0
 
Jeffrey CoachmanMIS LiasonCommented:
Congratulations!

Just keep in mind my comments about MS continual threatening to deprecating Send keys...
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
You may find issues with SendKeys in Win 7 ...

mx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.