Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Change sort order on a sub-form in VBA

I have a form that users copy/paste directly into.  The source data comes from another application that spits out in Excel.  The user then highligyhts the area needed and copies this directly into the sub-form (for various reasons, cannot upload).  So, now the data is in the sub-form, but I need a way for the user to be able to sort via a command button by Branch Number either ascendig or descending. Since there is no query or an open event, I cannot use these so must resort to a button.  I got as far as the OrderBy, but that does not work.  I simply want the data on the sub-form sorted either direction when the user hits the sort button.
Private Sub cmdBranchSort_Click()
    Me.sfmAMLItems.Form.OrderBy
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or

Me.sfmAMLItems.Form.OrderBy = "SomeFieldName DESC"

You either need two buttons (Asc or Desc) ... or ... retain the previous state and toggle it.  I would suggest two buttons.

mx
Avatar of Sandra Smith

ASKER

DatabaseMX works but now that the data is in ascending, would I set the OrderByOn = False?  Think I will go try that and see what happens.
Works, used the toggle as I am cramped for space.  As it toggles, the caption changes so the user knows which direction he is going.

Thank you.