Solved

Toggle Sort Button in Access 2003 Form

Posted on 2009-05-08
4
383 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 500 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

717 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