Solved

Toggle Sort Button in Access 2003 Form

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

829 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