DoCmd.SetOrderBy (Access)

Use the SetOrderBy method to apply a sort to the active datasheet, form, report, or table.

When you run this method, the sort is applied to the table, form, report, or datasheet (for example, query result) that is active and has the focus. The OrderBy argument is the name of the field or fields on which you want to sort records. When you use more than one field name, separate the names with a comma (,). The OrderBy property of the active object is used to save the ordering value and apply it at a later time. OrderBy values are saved with the objects in which they are created. They are automatically loaded when the object is opened, but they are not automatically applied. When you set the OrderBy argument by entering one or more field names and then run the method, the records are sorted by default in ascending order. To sort records in descending order, type DESC at the end of the OrderBy argument expression. For example, to sort customer records in descending order by contact name, set the OrderBy argument to "ContactName DESC". To sort names by LastName descending, and FirstName ascending, set the OrderBy argument to "LastName DESC, FirstName ASC"

SetOrderBy (OrderBy, ControlName)


DoCmd.SetOrderBy "LastName DESC, FirstName ASC"

Arguments

The following argument is required

OrderBy (String) - A string expression that includes the name of the field or fields on which to sort records and the optional ASC or DESC keywords.

Optional arguments

The following argument is optional

ControlName (String) - If provided and the active object is a form or report, the name of the control that corresponds to the subform or subreport that will be sorted. If empty and the active object is a form or report, the parent form or report is sorted.