Form Column OrderBy (1 Viewer)

kirkm

Registered User.
Local time
Tomorrow, 07:02
Joined
Oct 30, 2008
Messages
1,257
From Microsoft Help pages:
You can also set the OrderByOn property for either forms or reports by using VBA.
The following code example sorts the active datasheet, form, report, or table by LastName descending and FirstName ascending.

Code:
DoCmd.SetOrderBy "LastName DESC, FirstName ASC"
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.
However I am not finding success. Here what I have
Code:
     [Form_zqryShow subform].RecordSource = "tblCombined"
      [Form_zqryShow subform].SetFocus
      DoCmd.SetOrderBy "WeeksOn ASC"
      [Form_zqryShow subform].Refresh
The setFocus line creates error 2449 There is an invalid method in an expression.
If I rem this out (and the instructions says the Form needs the focus) the DoCmd line
shows Error 2046 The command or action 'SetOrderBy' isn't available now.
What is tne correct syntax to order by the WeeksOn column ?
 

June7

AWF VIP
Local time
Today, 11:02
Joined
Mar 9, 2014
Messages
5,465
You don't set focus to subform. Code acts on main form or report object that has focus. If you need to specify a subform or subreport to sort then use the ControlName argument.

"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."

However, I am having difficulty getting code to recognize name of subform container control.

Instead of SetOrderBy method, I would set OrderBy and OrderByOn properties, example:

Forms!Main.ctrRates.Form.OrderBy = "Rate DESC"
Forms!Main.ctrRates.Form.OrderByOn = True
 
Last edited:

kirkm

Registered User.
Local time
Tomorrow, 07:02
Joined
Oct 30, 2008
Messages
1,257
Thanks June I found an alternative way to get the desired result.
Code:
[Form_zqryShow subform].RecordSource = "Select * From tblCombined Order By WeeksOn;"
Perhaps obvious... in this case Googling only muddied the waters.
 

Users who are viewing this thread

Top Bottom