Results 1 to 14 of 14
  1. #1
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62

    OrderBy not working

    Hello,

    This should be a simple problem.
    I have a continuous form that I want to be able to open sorted by its primary key (Autonumber, long) ascending.


    I have the underlying table sorted by the primary key ascending.
    I have the "order by" property in the form's property sheet set to the string name of the primary key and the "order by on load" property set to "Yes".
    For good measure, I also have the VBA instructions Me.OrderBy = "PK" and Me.OrderByOn = True in the form's OnLoad event.
    Nevertheless, the form opens sorted by the primary key in descending order.

    Any ideas would be appreciated.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Here is an article which shows how to sort records in various Access objects (including Forms): https://support.office.com/en-sg/art...f-0fd8015008a8

    Quite frankly, I find the easiest way to do this is to sort the records in a Query, and then use the Query as the Record Source of the Form.
    Typically, I hardly ever use a Table as the Record Source of Forms or Reports. I use Queries (based on the Table), which allows me to easily change things, add things (especially calculated fields), add criteria, etc.

    It is pretty easy to do. Just create a Query based on your Table, adding all the fields, sorting, criteria you want. Then save the query.
    Then open your Form, go to Properties, go to the Data tab, and change the Record Source property to use your query instead of your table.

    Note that if you tried adding some sort options directly on your Form, you may need to remove them so that they don't try to "overwrite" your sort options in your query.

  3. #3
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    Yes, I've tried setting the recordsource to a query based on the table (i.e:

    SELECT SET_Order_Items_Services.* FROM SET_Order_Items_Services ORDER BY SET_Order_Items_Services.PK;

    but it still orders by the primary key in descending order.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    but it still orders by the primary key in descending order.
    Note that if you tried adding some sort options directly on your Form, you may need to remove them so that they don't try to "overwrite" your sort options in your query.
    You need to remove it from all places, Form Properties, VBA, etc.

    To prove that this works, try creating a new simple Form based on your sorted Query (can let the Autoform do it, if you like), and see what order it is sorted in.

  5. #5
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    I took your suggestion and removed the field name from the form's Order By property and also removed my VBA code from the form's OnLoad event. No difference. Using the query with the Order By statement, it still sorts by the primary key descending.

    I don't know if this matters but this form is used as a subform but the main form doesn't tell it how to sort anywhere.

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It sounds to me like you have something still affecting the sorting on your existing form that you just haven't located.

    What happens if you try my suggestion to create that Form again from scratch?
    Does it then sort in the right order?

  7. #7
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    I created a brand-new form with the same result. Sorry.

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Can you post the SQL code of your query?
    And let us know which field you are trying to sort by?

    Note that you have the ability to upload your database to this site. That might go a much longer way in diagnosing the issue, as then we will be able to see all the components. If you do that, just be sure to remove any sensitive/private information.

    Also, I am unable to download files from my current location, but could do so tonight.

  9. #9
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    The form's recordsource SQL statement is:

    SELECT SET_Order_Items_Services.* FROM SET_Order_Items_Services ORDER BY SET_Order_Items_Services.PK;

    Where PK is the autonumbered primary key.

  10. #10
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Don't think I have any more suggestions without being able to see it.

  11. #11
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    I'll try to put something together and upload it. Meanwhile I found an interesting posting:

    https://social.msdn.microsoft.com/Fo...orum=accessdev

    I don't use a datasheet but there seems to be some inherent problem somewhere.

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Euler,

    What happens when you run your query in the query window SQL view?

    Code:
    SELECT SET_Order_Items_Services.* FROM SET_Order_Items_Services ORDER BY SET_Order_Items_Services.PK;
    You could have some corruption somwhere. You could try a compact and repair. No harm in my view.

  13. #13
    Euler is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2015
    Posts
    62
    The query works properly when run as a stand-alone query. It sorts ascendingly. It just seems to be a problem when opening the subform that it doesn't behave the way it should.

  14. #14
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It sounds like you may be experiencing the issue mentioned in that link you posted.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How Can I: OrderBy inside an Other OrderBy
    By RichardAnderson in forum Forms
    Replies: 1
    Last Post: 08-23-2013, 01:04 PM
  2. Turn Off/Prevent OrderBy - How?
    By rsACC99 in forum Forms
    Replies: 1
    Last Post: 04-03-2013, 11:14 AM
  3. Me.OrderBy not working
    By Rramjet in forum Forms
    Replies: 5
    Last Post: 03-15-2013, 11:13 AM
  4. OrderBy Not working for select few cases
    By walter189 in forum Programming
    Replies: 2
    Last Post: 08-19-2011, 05:53 AM
  5. OrderBy Losing Criteria
    By P5C768 in forum Forms
    Replies: 1
    Last Post: 01-21-2011, 01:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums