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, 2 months 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
-
Turning off OneDrive
by
CWBillow
40 minutes ago -
June 2025 updates are out
by
Susan Bradley
26 minutes ago -
Mozilla shutting Deep Fake Detector
by
Alex5723
10 hours, 57 minutes ago -
Windows-Maintenance-Tool (.bat)
by
Alex5723
11 hours, 1 minute ago -
Windows 11 Insider Preview build 26200.5641 released to DEV
by
joep517
13 hours, 30 minutes ago -
Windows 11 Insider Preview build 26120.4250 (24H2) released to BETA
by
joep517
13 hours, 31 minutes ago -
Install Office 365 Outlook classic on new Win11 machine
by
WSrcull999
13 hours, 29 minutes ago -
win 10 to win 11 with cpu/mb replacement
by
aquatarkus
5 hours, 21 minutes ago -
re-install Windows Security
by
CWBillow
16 hours, 46 minutes ago -
WWDC 2025 Recap: All of Apple’s NEW Features in 10 Minutes!
by
Alex5723
20 hours, 27 minutes ago -
macOS Tahoe 26
by
Alex5723
14 hours, 40 minutes ago -
Migrating from win10 to win11, instructions coming?
by
astro46
2 hours, 45 minutes ago -
Device Eligibility for Apple 2026 Operating Systems due this Fall
by
PKCano
5 hours, 13 minutes ago -
Recommended watching : Mountainhead movie
by
Alex5723
5 hours, 57 minutes ago -
End of support for Windows 10
by
Old enough to know better
5 hours, 53 minutes ago -
What goes on inside an LLM
by
Michael Covington
6 minutes ago -
The risk of remote access
by
Susan Bradley
5 hours, 1 minute ago -
The cruelest month for many Office users
by
Peter Deegan
12 hours, 9 minutes ago -
Tracking protection and trade-offs in Edge
by
Mary Branscombe
10 hours, 11 minutes ago -
Supreme Court grants DOGE access to confidential Social Security records
by
Alex5723
1 day, 18 hours ago -
EaseUS Partition Master free 19.6
by
Alex5723
19 hours, 28 minutes ago -
Microsoft : Edge is better than Chrome
by
Alex5723
2 days, 8 hours ago -
The EU launched DNS4EU
by
Alex5723
2 days, 20 hours ago -
Cell Phone vs. Traditional Touchtone Phone over POTS
by
280park
2 days, 11 hours ago -
Lost access to all my networked drives (shares) listed in My Computer
by
lwerman
3 days, 2 hours ago -
Set default size for pasted photo to word
by
Cyn
3 days, 8 hours ago -
Dedoimedo tries 24H2…
by
Cybertooth
2 days, 20 hours ago -
Windows 11 Insider Preview build 27871 released to Canary
by
joep517
4 days, 7 hours ago -
Windows 11 ad from Campaign Manager in Windows 10
by
Jim McKenna
2 days ago -
Small desktops
by
Susan Bradley
11 hours, 42 minutes 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.