Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 35
  1. #16
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33

    The first to sections are associated to a category of objects, while the third is an incremental numbering.

  2. #17
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Write a function that processes and returns the codes properly formatted then sort by that. It's only needed for sorting and could be disregarded otherwise. Have it return something like "xxxxxyyyyyzzzzz", each of the three sections have extra leading zeros to cover odd case that has more digits , and if a group is missing plug in all zeros for that group.

  3. #18
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Trial4life,

    It seems that your requirements have not been identified in full.

    -The first to sections are associated to a category of objects, while the third is an incremental numbering.
    -But the middle part of the field ("YYY") can be either of 2 or 3 digits.
    -Okay it seems that I managed to make it work!
    -This can't be a solution for my situation because these codes are associated with official document names, and sometimes they have leading zeros, sometimes not.

    I suggest you take some real life examples - representing the various configurations.
    List them, and then tell/show us what the proper sorting result should be.

    People are offering well intended solutions to a "changing or incomplete requirement".

    Good luck.

  4. #19
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33
    Right, I'll post the entire all of the possible configurations of my field I'm trying to sort, so maybe it will be more clear what I'm trying to do:

    Code:
    XXXX
    XXXX-YY
    XXXX-Y
    XXX-YY
    XXX-YY
    XXX-YYY
    XXX-Y-Z
    XXX-Y-ZZ
    XXX-Y-ZZ
    XXX-YY-Z
    XXX-YY-ZZ
    XXX-YY-ZZZ
    XXX-YYY-Z
    XXX-YYY-ZZ
    XXX-YYY-ZZZ
    As you can see, the field is made of heterogeneous data. To generalize to result, the best thing would be to split the string each time a "-" is encountered, and if there is only one "-" (or even none), format it into a "xxxxxyyyyyzzzzz", as kd2017 suggested.

  5. #20
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Courtesy of chatgpt:

    Click image for larger version. 

Name:	test.png 
Views:	26 
Size:	51.9 KB 
ID:	50248

  6. #21
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    I asked chatgpt to modifiy the function to handle missing groups of numbers:

    Code:
    Function FormatString(inputString As String) As String
        Dim groups() As String
        Dim formattedString As String
        Dim i As Integer
        
        ' Split the input string into groups
        groups = Split(inputString, "-")
        
        ' Format each group with zero left padding up to 4 digits
        For i = LBound(groups) To UBound(groups)
            groups(i) = Format$(Val(groups(i)), "0000")
        Next i
        
        ' Check if second group is missing and add "0000" at the end
        If UBound(groups) < 1 Then
            ReDim Preserve groups(1)
            groups(1) = "0000"
        End If
        
        ' Check if third group is missing and add "0000" at the end
        If UBound(groups) < 2 Then
            ReDim Preserve groups(2)
            groups(2) = "0000"
        End If
        
        ' Concatenate the groups with hyphens
        formattedString = Join(groups, "-")
        
        FormatString = formattedString
    End Function

  7. #22
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33
    Many thanks, that could definitely work!

    I'm just trying to figure out how to use this function in the advanced sorting... do I have to use another VBA sub that calls the FormatString function?

  8. #23
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    Quote Originally Posted by Trial4life View Post
    Many thanks, that could definitely work!

    I'm just trying to figure out how to use this function in the advanced sorting... do I have to use another VBA sub that calls the FormatString function?
    You would just call the function from the query and sort by that column like so:
    Click image for larger version. 

Name:	p0.png 
Views:	23 
Size:	19.6 KB 
ID:	50249

    produces this:
    Click image for larger version. 

Name:	p1.png 
Views:	23 
Size:	17.3 KB 
ID:	50250

    *Note: So this is the sql engine making a call to a vba function one time for every record returned, calling vba from queries like this can be inefficient. Generally speaking I try not to do this very often, if I do it's on a filtered/limited number of rows. If there are sql native ways to accomplish something, I usually try to do that first.

  9. #24
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33
    I've been trying the exact same thing for the last hour, but I always get the following error:

    Code:
    "Undefined function "FormatString" in expression"
    What am I doing wrong? It seems like the query is not recognizing the function (I've placed it into a dedicated module).

    Regarding your note, the sorting will always run on a limited amount of records, so probably it won't be an issue. Anyways, is it possible to achieve the same result only using SQL?

  10. #25
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33
    Okay, it turned out that Access didn't like the function name "FormatString" (I checked and it was the only function with that name in the entire database... so I have no clue why); however, renaming that function to "FormatStringV2" everything worked fine.

    Thank you again for your help and your patience!

  11. #26
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,165
    You're welcome. And good work figuring out the naming issue. The name FormatString is awfully generic, if I had written it I probably would have named it something a bit more specific. Also, I was going to suggest maybe specifying Public Function instead of just Function.
    Last edited by kd2017; 05-17-2023 at 07:14 AM.

  12. #27
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33
    Strange thing: I get the same error if I rename the .ACCDB file and reopen it. What could be causing this issue?

  13. #28
    CarlettoFed is offline Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2019
    Posts
    274
    Maybe if you attach an example file it's easier to understand what's going on and help you.

  14. #29
    Trial4life is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2022
    Posts
    33
    I've figured it out: every time I rename the .ACCDB file (or if I move it somewhere else), the first time I reopen it, it blocks the macros and asks me if the file is trusted. Before I accept and continue, the function "Format_String" is still not defined (since the macros are dsabled), and I get the error messages.

    https://imgur.com/ZogKPVN

    Is there a way to bypass this procedure (or at list prevent the "Format_String" error window from showing up)?



  15. #30
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,205
    @kd2017
    I've just skimmed this thread including post #26
    For info, writing Public Function is functionally equivalent to writing Function. All functions in a standard/class module are public unless specifically marked as Private

    @Trial4life
    Place the file in a trusted location
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Having trouble getting a query to sort properly
    By S2000magician in forum Queries
    Replies: 6
    Last Post: 07-17-2019, 02:05 PM
  2. Replies: 2
    Last Post: 02-10-2015, 02:12 PM
  3. Replies: 9
    Last Post: 11-14-2014, 06:11 PM
  4. Replies: 10
    Last Post: 05-09-2014, 10:54 AM
  5. Replies: 4
    Last Post: 08-14-2012, 10:33 AM

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