Solved

Sort buttons in access 2003 FORM

Posted on 2009-05-08
8
483 Views
Last Modified: 2013-11-28
How do I create a command button in an access FORM to sort ascending and descending?

I tried using the runcommand with ascending/descending, but access 2003 says it cannot run that command at this time.
0
Comment
Question by:jbradford777
  • 4
  • 4
8 Comments
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
What do you want it sorted by? by any field on the screen or selected fields?

if u right click on a field, there is already a sort option
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
to order in vba, you do this

Me.OrderBy = "MyFieldName"
Me.OrderByOn = True

'descending is
Me.OrderBy = "MyFieldName DESC"


0
 

Author Comment

by:jbradford777
Comment Utility
OK, I realize there are sort buttons already on a form, however I need to add buttons for people who don't use access...no right clicking, no nothing, just a button that tells them they can sort.  It's to sort by any field in the form.  I just started writing VBA, so, I really don't know how to start or finish the code you placed above???
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
Comment Utility
ok, a form can have several fields right? lets say you want to filter on one or more. But to avoid confusion as what the sort is, maybe a button next to each textbox that you want to sort on.

So create a button, lets say its called txtFred. The fieldname in this table is called Wilma. So on the click event of txtFred, you would want to sort by Wilma.
If you want it toggable so it orders by ascending or descending then we simply check the current order value

eg

private sub txtFred_Click()

    if Me.OrderBy = "Wilma" then
        Me.OrderBy = "Wilma DESC"
    else
        Me.OrderBy = "Wilma"
    end if
    Me.orderbyon =true
end sub
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jbradford777
Comment Utility
OK, what if it's highest cost vs. lowest cost (not a text field, it's a number)

The field name is 4-Week Total Cost...do I put it in [4-Week Total Cost] brackets or "4-Week Total Cost"?
0
 

Author Comment

by:jbradford777
Comment Utility
oh, here's the code...it works, but it only sorts one way, how do you put the toggle in to switch from asc to desc.?

Private Sub Command72_Click()
   
    If Me.OrderBy = [4-Week Total Cost] Then
        Me.OrderBy = [4-Week Total Cost DESC]
    Else
        Me.OrderBy = "4-Week Total Cost"
    End If
    Me.OrderByOn = True

End Sub
0
 

Author Closing Comment

by:jbradford777
Comment Utility
THANK YOU!!!!!!!!!!!!!!!!!!!
0
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
Needs to be in quotes and text consistent


If Me.OrderBy = "[4-Week Total Cost]" Then
        Me.OrderBy = "[4-Week Total Cost DESC]"
    Else
        Me.OrderBy = "[4-Week Total Cost]"
    End If
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now