Results 1 to 15 of 15
  1. #1
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21

    Report Sorting Issue

    Hey everyone,

    I am relatively new to Access and have been setting up a pretty simple database to track some things at work. I have been able to figure most everything out except a sorting issue in a report that I need. I am sorting by semester, ie spring 2011, summer 2011, fall 2011, spring 2012 ect. My problem is I need them to go in that order, by the academic calendar. I cannot figure out how to sort by anything other than alphabetically and I do not want to add random characters to make it sort correctly. Can what I want to do be done?

    Thanks!



    Calvin

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    I'm not above brute force when I know it works.

    Quote Originally Posted by cbgroves View Post
    Hey everyone,

    I am relatively new to Access and have been setting up a pretty simple database to track some things at work. I have been able to figure most everything out except a sorting issue in a report that I need. I am sorting by semester, ie spring 2011, summer 2011, fall 2011, spring 2012 ect. My problem is I need them to go in that order, by the academic calendar. I cannot figure out how to sort by anything other than alphabetically and I do not want to add random characters to make it sort correctly. Can what I want to do be done?

    Thanks!

    Calvin
    Add a calculated field to the query on which your report is based.
    Assuming that you have the semester already present, use an iif statement to enumerate the semester information in the desired order, i.e. 20111, 20112, 20113 or whatever scheme makes sense to you. Then sort on it. You don't need to print it on the report, although it may have to be hidden in the detail somewhere.

  3. #3
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    Would it be possible for you to write me an example of how this would look? I would greatly appreciate it!

  4. #4
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    Select the query which you wish to sort. Go to design view.

    Assuming that the query already contains a column 'Semester', (I'm abbreviating as SM to make this more readable), go to a blank column and insert the following:

    SortFld:iif( INSTR(0,[SM],"Winter",),Right([SM],4)&"1", iif(INSTR(0,[SM],"Spring",),Right([SM],4)&"2",iif( INSTR(0,[SM],"Summer",),Right([SM],4)&"3",Right([SM],4)&"4" ) ) )

    All on one line.
    The above coding could be shortened to
    SortFld:iif( INSTR(0,[SM],"W",),Right([SM],4)&"1", iif(INSTR(0,[SM],"Sp",),Right([SM],4)&"2",iif( INSTR(0,[SM],"Su",),Right([SM],4)&"3",Right([SM],4)&"4" ) ) )

    Since this hasn't been checked by Access yet, you may expect problems with unbalanced parens, or commas in the wrong spot. Just try to get the gist of the code.
    When you run the query, you should get a column of calculated values similar to
    20111
    20112
    20113
    20114
    20121

    If the order of the semesters is something other than winter = 1, spring = 2... then you will need to switch either the test string or the appended string around.
    Note that if you have trailing blanks in the list of semesters, you may have to TRIM the variable.

  5. #5
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    Thanks, I think I'm getting close! I am pretty sure I understand the syntax here, but I am getting #Func! as the output for all semesters. I don't know if it makes a difference, but the four semesters a year I am using are: Spring, Summer 4 Week, Summer 8 Week, and Fall. I appreciate the help!

  6. #6
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    Here is the code I have now:

    SortFld: IIf(InStr(0,[Semester to Deliver],"Spring"),Right([Semester to Deliver],4) & "1",IIf(InStr(0,[Semester to Deliver],"Summer 4 Week"),Right([Semester to Deliver],4) & "2",IIf(InStr(0,[Semester to Deliver],"Summer 8 Week"),Right([Semester to Deliver],4) & "3",Right([Semester to Deliver],4) & "4")))

    Semesters are formatted as (with no spaces before or after): Spring 2011, Summer 8 Week 2011 ect.

  7. #7
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    Evidently I lied. I just linked it to my table and now I get a syntax error (comma) Can you spot my error, because I cant :/

  8. #8
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    I put a comma after the "test string" in INSTR

    Some of the descriptions show examples with the last parameter and comma omitted, but I think it is necessary.

  9. #9
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    I'm not sure I know how to correct that...

  10. #10
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    From the code you posted, I've added commas.

    SortFld: IIf(InStr(0,[Semester to Deliver],"Spring",),Right([Semester to Deliver],4) & "1",IIf(InStr(0,[Semester to Deliver],"Summer 4 Week",),Right([Semester to Deliver],4) & "2",IIf(InStr(0,[Semester to Deliver],"Summer 8 Week",),Right([Semester to Deliver],4) & "3",Right([Semester to Deliver],4) & "4")))

    The commas are just before the closing parenthesis of each INSTR.

  11. #11
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    I added the three commas you added and I get the same comma error.

  12. #12
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    Actually when I add those commas and try and run the report I get the error, so when I go back to look at the formula again the commas are gone.

  13. #13
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    I don't see anything that I think is an error, so how about a desperate

    guess:

    SortFld: IIf(InStr(0,[Semester to Deliver],"Spring",1),(Right([Semester to Deliver],4) & "1"),IIf(InStr(0,[Semester to Deliver],"Summer 4 Week",1),(Right([Semester to Deliver],4) & "2"),IIf(InStr(0,[Semester to Deliver],"Summer 8 Week",1),(Right([Semester to Deliver],4) & "3"),(Right([Semester to Deliver],4) & "4"))))

    BTW, I urge you to avoid using spaces and special characters in your names.

    If this fails, fall back to seeing if the first test works,i.e. take out the second and third iifs, and narrow done what is failing.

    I'll get on my Access box and try this.

  14. #14
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    This works

    SortFld: IIf(InStr(1,[Sem],"Spring",1),(Right([Sem],4) & "1"),IIf(InStr(1,[Sem],"Summer 4 Week",1),(Right([Sem],4) & "2"),IIf(InStr(1,[Sem],"Summer 8 Week",1),(Right([Sem],4) & "3"),(Right([Sem],4) & "4"))))

    The start comparison position is '1', not 0.
    Apologies.

  15. #15
    cbgroves is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2011
    Posts
    21
    That worked! Thanks so much!

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

Similar Threads

  1. Replies: 7
    Last Post: 07-10-2011, 06:55 PM
  2. Incorrect sorting in report.
    By jonesy29847 in forum Reports
    Replies: 2
    Last Post: 06-16-2010, 05:56 PM
  3. Sorting a Report by a Calculated field
    By mulefeathers in forum Reports
    Replies: 1
    Last Post: 05-22-2010, 08:21 PM
  4. sorting a field in report
    By Philangr8 in forum Reports
    Replies: 3
    Last Post: 08-26-2009, 05:38 PM
  5. Sorting data in a report
    By rjsiler in forum Reports
    Replies: 1
    Last Post: 08-04-2008, 01:40 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