[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Toggle Sort Button in Access 2003 Form

Posted on 2009-05-08
4
Medium Priority
?
386 Views
Last Modified: 2013-11-27
Need code to toggle between sorting ascending and descending...the code below is only sorting ascending
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

Open in new window

0
Comment
Question by:jbradford777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 24338805
try
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 asc"
    End If
    Me.OrderByOn = True
 
End Sub

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 24339007
jbradford777,

1. change the caption of your command72 button to ASC
save the changes

2. try this codes

Private Sub Command72_Click()
    if me.Command72.caption="ASC" then
         Me.OrderBy = "4-Week Total Cost"
         me.Command72.caption="DESC"
         else
          Me.OrderBy = "4-Week Total Cost desc"
         me.Command72.caption="ASC"
  end if
      Me.OrderByOn = True
 
End Sub
0
 

Author Closing Comment

by:jbradford777
ID: 31579577
AWESOME!!!  THANK YOU SO MUCH!
0
 
LVL 11

Expert Comment

by:LambertHeenan
ID: 24339712
The reason you are having trouble is that your field name has spaces in it. So when you first open the form OrderBy is a blank string "".

Then when this line executes the first time you click the button,

Me.OrderBy = "4-Week Total Cost"

the form gets sorted ascending, but Access sets OrderBy to "[4-Week Total Cost]", automatically inserting the square brackets. As a result the expression

Me.OrderBy = "4-Week Total Cost"

is always false, so your code never sorts descending. Change your code to this...

    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


and it will work.

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

656 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