none
order by an object's control source RRS feed

  • Question

  • can I order records in a form by an object's control source via vba? (the control source is variable)

    thanks


    • Edited by moaaz-96 Sunday, April 9, 2017 4:33 AM
    Saturday, April 8, 2017 7:44 PM

Answers

  • You should be able to use

        Me.OrderBy = "[" & Me.ComboBoxName.Value & "]"

    where ComboBoxName is the name of your combo box.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by moaaz-96 Monday, April 10, 2017 5:38 AM
    Sunday, April 9, 2017 4:11 PM

All replies

  • If the control source is a field, yes:

        Me.OrderBy = "FieldName"
        Me.OrderByOn = True


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, April 8, 2017 8:01 PM
  • Hans Vogelaar MVP

    yes its a field but its also a variable

    Sunday, April 9, 2017 4:32 AM
  • You can set the OrderBy property to a calculated expression, e.g.

        Me.OrderBy = "[Quantity]*[Amount]"

    The expression must use the names of fields in the record source of the form. You cannot refer to the names of controls in the expression.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, April 9, 2017 9:28 AM
  • Hans Vogelaar MVP

    the only way to sort records everytime we change the control source is manually , isnt it ?

    Sunday, April 9, 2017 10:46 AM
  • Why are you changing the control source of a control?

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Sunday, April 9, 2017 11:05 AM
  • Why are you changing the control source of a control?

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    because in my table I have many fields , i want to create a report for every field in my table with (name) field .. and for this .. i have to create a query for every field in the table so i created one report contains a combobox wich field i want to use and this combobox can change the control source after update .. everything worked but the sorting i didnt know how to do it when user change the control source
    Sunday, April 9, 2017 2:22 PM
  • You should be able to use

        Me.OrderBy = "[" & Me.ComboBoxName.Value & "]"

    where ComboBoxName is the name of your combo box.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by moaaz-96 Monday, April 10, 2017 5:38 AM
    Sunday, April 9, 2017 4:11 PM
  • Why are you changing the control source of a control?


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    because in my table I have many fields , i want to create a report for every field in my table with (name) field .. and for this .. i have to create a query for every field in the table so i created one report contains a combobox wich field i want to use and this combobox can change the control source after update .. everything worked but the sorting i didnt know how to do it when user change the control source
    Sounds like you have a spreadsheet table rather than a relational database.

    Build a little, test a little

    Sunday, April 9, 2017 4:19 PM
  • You should be able to use

        Me.OrderBy = "[" & Me.ComboBoxName.Value & "]"

    where ComboBoxName is the name of your combo box.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    this is exactly what i need :D thank you very much!
    Monday, April 10, 2017 5:38 AM