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

    .OrderBy Clause problem


    Having some trouble getting this to work. My data is formatted A1, A2, B3 etc but where the number part is 10
    this comes before 1.
    I have tried changing it in a Function e.g.
    Code:
    Function func(at) As Integer
    If Nz(at) > "" Then
    func = Asc(at) + Mid(at, 2)
    Else
    func = 0
    End If
    But I can't get it sorting correctly. The function is working because if I have func= mid(at,2) it sorts all 1's first then 2's etc but of course A,B,C part is wrong.

  2. #2
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348
    Got it working.
    Code:
    Function func(at) As String
        If Nz(at) > "" Then
            func = Left(at, 1) & Chr$(Mid(at, 2))
        Else
            func = "0"
        End If
    End Function

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Did you mean 10 comes before 2? This would be expected because this is alpha sort - one character at a time is considered in the ordering. If data had been saved as A001, A002 ... A010, sort would be correct. This is why discrete data should be stored in separate fields, not mushed together.

    Need to parse the alpha and number parts to separate values for sorting purpose. Will there always be one alpha character prefix?

    SELECT * FROM sometable ORDER BY Left(field, 1), Val(Mid(field, 2));
    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.

  4. #4
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348
    @June7 Thank you. Yes always one alpha char.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Middlemarch View Post
    @June7 Thank you. Yes always one alpha char.
    Famous last words
    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

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

Similar Threads

  1. Problem with Alias in WHERE clause
    By skyrat in forum Access
    Replies: 2
    Last Post: 12-24-2019, 03:11 PM
  2. Problem With Group by clause in a Query
    By deepakg27 in forum Queries
    Replies: 1
    Last Post: 01-15-2018, 11:35 PM
  3. How Can I: OrderBy inside an Other OrderBy
    By RichardAnderson in forum Forms
    Replies: 1
    Last Post: 08-23-2013, 01:04 PM
  4. Problem With WHERE Clause
    By zephaneas in forum Queries
    Replies: 1
    Last Post: 08-17-2011, 01:54 PM
  5. Problem Adding WHERE Clause
    By zephaneas in forum Queries
    Replies: 1
    Last Post: 08-14-2011, 11:26 PM

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