I want to put a cmd button on my form to allow the user to ‘sort ascending’ the table on the form field that has the focus.
The process works great if I use the Records, Sort, Sort Ascending selections from the Menu bar but I can not make it work
using code. I will appreciate any suggestions. Thank you.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Visual Basic code to Sort (A2K)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Visual Basic code to Sort (A2K)
- This topic has 8 replies, 4 voices, and was last updated 22 years, 1 month ago.
AuthorTopicWSlfwatson
AskWoody LoungerApril 14, 2003 at 4:34 pm #386079Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerApril 14, 2003 at 6:02 pm #668458The problem is that when you click the command button, the button gets the focus, so there is no field that has the focus any more,
As an alternative, you can put a combo box on the form with Row Source Type set to List of Fields and Row Source set to the name of the table or query that acts as record source of the form. In the AfterUpdate event of the combo box, you can set the OrderBy property of the form to the selected field, and the OrderByOn property to True
If you don’t want to use a combo box, you must use a keyboard shortcut or a toolbar button that reads the Control Source of the ActiveControl, and orders the form by that field. You can set the KeyPreview property of the form to True, and handle keystrokes at the form level instead of for each control separately.
-
WShasse
AskWoody LoungerApril 15, 2003 at 3:57 pm #668462In attachment you find an example using a menu button, which calls a macro (which calls a function) sorting your form on the active control. I created two buttons, for ascending & descending sort order respectively.
You still need to convert it from Access 97 to 2K but that shouldn’t raise any problems (!?). It’s the first time I put an attachment on this forum. I hopes it works…
Greetings,
Hasse -
WShasse
AskWoody LoungerApril 15, 2003 at 4:03 pm #668463In case anyone should be interested in the code (stored in the module), which, I presume, is moreless what HansV suggested:
‘Sort active form ascending using active control
Public Function ffrmSortAsc()
Dim frm As Form, ctl As Control
Set frm = Screen.ActiveForm
Set ctl = frm.ActiveControl
frm.OrderBy = ctl.ControlSource
frm.OrderByOn = True
End Function‘Sort active form descending using active control
Public Function ffrmSortDesc()
Dim frm As Form, ctl As Control
Set frm = Screen.ActiveForm
Set ctl = frm.ActiveControl
frm.OrderBy = ctl.ControlSource & ” DESC”
frm.OrderByOn = True
End Function -
WSlfwatson
AskWoody Lounger -
WShasse
AskWoody LoungerApril 15, 2003 at 4:10 pm #668729Hey, Ifwatson,
I’m happy I could, for a change, help someone else too :-).
Please notice that I changed my posts: I cut the code of the second function procedure, which factually wasn’t needed at all.
Just one last question, out of curiosity: why do you want to sort using code instead of the available Access sort buttons?
Hasse -
WSlfwatson
AskWoody LoungerApril 15, 2003 at 8:03 pm #668780Hasse,
The reason for the command button is that my users are NOT very savvy with Access and by putting everything they need to do on the screen it makes them have to think even less. I have been coding Access inhouse applications for about 10 years. Before Access I created applications with dBase. If I can be nosey, are you an application programmer?
Thank you again. -
WShasse
AskWoody LoungerApril 16, 2003 at 11:13 am #668935Hey, Ifwatson,
honestly, ‘programmer’ might still be a bridge too far in my case. For example, I think I still lack some automatisms skilled IT-people have,… But I’m able to do moreless what I want in VBA. Access is more a hobby to me, but as I’m having the opportunity to amuse myself with it (he he) at my job as well, I-m a bit a professional as well (though I think most of us can say that :-).Thanks both for telling why ‘doing on your own’ what Access also offers, is sometimes unavoidable.
And by the way, Charlotte, thanks for your tip. Often, many ways lead to Rome but this is a neat suggestion, especially when you use continuous forms.
Hasse
-
WScharlotte
AskWoody LoungerApril 16, 2003 at 2:54 am #668867It’s quite common to provide custom methods for users to sort data because the built in menus are not usually available in finished applications. One technique I use is the click event of the label for the column. In that event, I set the OrderBy property of the form to the associated field and then set the OrderBy on. Users don’t have to know about anything except that clicking the label will sort by that column. I usually set it up so that the sort toggles. If they click the label again, the sort reverses itself between ascending and descending. Many users have never figured out what the sort buttons are for even if they are visible.
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Sometimes I wonder about these bots
by
Susan Bradley
3 hours, 20 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
5 hours, 18 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
6 hours, 18 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
5 hours, 43 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
2 hours, 13 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
9 hours ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
9 hours, 1 minute ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
2 hours, 54 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
17 hours, 9 minutes ago -
0Patch, where to begin
by
cassel23
11 hours, 11 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 6 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
18 hours, 32 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 15 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 6 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
17 hours, 26 minutes ago -
Installer program can’t read my registry
by
Peobody
6 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 4 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 11 hours ago -
False error message from eMClient
by
WSSebastian42
2 days, 2 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 11 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 12 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
23 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 16 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 17 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 21 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 3 hours ago -
Another big Microsoft layoff
by
Charlie
3 days, 2 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
4 hours, 55 minutes ago -
May 2025 updates are out
by
Susan Bradley
6 hours, 36 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 8 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.