Results 1 to 7 of 7
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    374

    QrderBy both?


    A string field may contain A1, A2, B2 etc or 1,2,3, 10, 11
    Is there any way to Order these both correctly in the same query?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    Not without modifying the data. All these values are text and alpha sort rules apply. 10 will sort before 3, numbers before letters.
    What would be correct order? Numbers first?
    Could the alpha-prefix values have multiple digits?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    374
    The correct order is text sort, except when numbers.
    Yes, adding a leading "0" works. But a big change.
    You gave me an idea though and adding this function into the query seems to work.
    Code:
    Function zeroit(x)
        zeroit = x
        If Len(x) = 1 Then
            If Val(x) > 0 Then zeroit = "0" & x
        End If
    End Function

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    So values will always be only 1 or 2 characters?

    Custom VBA function is not needed.

    SELECT * FROM table ORDER BY Format(field, "00");
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    374
    It's always good to see someone else's approach.
    I tried ?format(199,"00") in the immediate window and it printed 199.
    I tested in the table and found it put 199 before 21. But changing to Format(field, "000"); solved that.
    Then I found Values are never more than 99 so I didn't need the extra 0.
    Thanks June, nice solution and better than I had.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,016
    Format returns a string, so 199 will always come before 21 if sorted ascending.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    @Weshgasman, not with leading placeholder zeros which is how this Format function call is structuring numbers.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

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