Link to home
Start Free TrialLog in
Avatar of Rick Danger
Rick DangerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

vba order by

I have the following code:
Me.OrderBy = "person_id"
Me.OrderByOn = True

where person_id is the form field's control source. However, I wish to use its name instead. Is there a way to do this.

So, for clarity, in the form design, the field on the form has a control source of "person_id" (from the "data" tab), but its name is "field_1" (from the "other" tab).
Avatar of acbxyz
acbxyz
Flag of Germany image

Not as a string, but you can use field_1.ControlSource without quotes. field_1 is the object of your control element and its property ControlSource contains the string.

Me.OrderBy = field_1.ControlSource
Avatar of Scott McDaniel (EE MVE )
Why not just:

OrderBy = "Field_1"

The OrderBy property tells Access how you'd like to Order your records (obviously). It expects a Field name as the setting.
Why would you care?  And why would you use meaningless values as the Name for a control?  When you use the wizard or when you drag fields from the field list, Access assigns the bound field name as the control name.  When you click on a control in the ribbon and draw it on the form/report, Access assigns an arbitrary name.  You should then change that to something meaningful.  Who wants to look at code that references text038 and chk120?
" I wish to use its name instead. "
The OrderBy property requires the name of a Field(s) in the underlying Recordsource of the Form, not the Name of a Control.

mx
Avatar of Rick Danger

ASKER

acbxyz - thanks for your answer, It works, so I'l happily give you the points, but how would I write this code if I also wanted to choose to sort ascending or descending?
ASKER CERTIFIED SOLUTION
Avatar of acbxyz
acbxyz
Flag of Germany 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
That's brilliant. Thanks.
Excellent solution, thanks very much.