편집

다음을 통해 공유


DoCmd.SetOrderBy method (Access)

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

Syntax

expression.SetOrderBy (OrderBy, ControlName)

expression A variable that represents a DoCmd object.

Parameters

Name Required/Optional Data type Description
OrderBy Required Variant A string expression that includes the name of the field or fields on which to sort records and the optional ASC or DESC keywords.
ControlName Optional Variant 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.

Remarks

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"

Example

The following code example sorts the active datasheet, form, report, or table by LastName descending and FirstName ascending.

DoCmd.SetOrderBy "LastName DESC, FirstName ASC"

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.