Solved

Toggle Sort Button in Access 2003 Form

Posted on 2009-05-08
4
378 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
Comment Utility
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 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
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
Comment Utility
AWESOME!!!  THANK YOU SO MUCH!
0
 
LVL 11

Expert Comment

by:LambertHeenan
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

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

11 Experts available now in Live!

Get 1:1 Help Now