Results 1 to 15 of 15
  1. #1
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496

    sort order of the date is "slightly" off...

    I want to sort this query first by last 'date visited'; then by 'times visited'

    the problem seems to be the way the date is sorted
    in descendnig order, I get:
    6/8/2015
    6/5/2015
    6/4/2015


    6/12/2015

    it seems to be sorting the date as text, NOT as a date
    so I poked around, and change the query to adress date sorting (I thought)...
    I suspect, as my grandmother would say: "right church, wrong pew..."

    here's the SQL

    SELECT
    [tblChangeProjectList].[ProjectName],
    [tblChangeProjectList].[FilePath],
    [tblChangeProjectList].[FilePath],
    [tblChangeProjectList].[TimesVisited],
    TO_CHAR( [tblChangeProjectList].[LastTimeVisited],'YYYYMMDD') LastTimeVisited
    FROM tblChangeProjectList ORDER BY [ProjectName]
    UNION Select "< Pick from List...>", "<PICK...>", "", "", ""
    FROM [tblChangeProjectList]
    ORDER BY LastTimeVisited DESC , [tblChangeProjectList].[TimesVisited];


    many thanks in advance,
    Mark

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Perhaps you need to replace TO_CHAR with TO_DATE
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    the problem seems to be the way the date is sorted
    in descendnig order, I get:
    6/8/2015
    6/5/2015
    6/4/2015
    6/12/2015

    it seems to be sorting the date as text, NOT as a date
    Actually, you can do this pretty simply using the DateValue function in your "Order By" clause like this:
    SELECT
    [tblChangeProjectList].[ProjectName],
    [tblChangeProjectList].[FilePath],
    [tblChangeProjectList].[FilePath],
    [tblChangeProjectList].[TimesVisited],
    [tblChangeProjectList].[LastTimeVisited]
    FROM tblChangeProjectList
    ORDER BY [ProjectName],
    DateValue([LastTimeVisited]) DESC

  4. #4
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    I forgot to include that the feild is already stored in a date format (general date format)

  5. #5
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    chonged to be:

    SELECT [tblChangeProjectList].[ProjectName], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[TimesVisited], [tblChangeProjectList].[LastTimeVisited] FROM tblChangeProjectList ORDER BY [ProjectName] UNION Select "< Pick from List...>", "<PICK...>", "", "", "" FROM [tblChangeProjectList]
    ORDER BY DateValue([tblChangeProjectList].[LastTimeVisited]) DESC , [tblChangeProjectList].[TimesVisited];

    which results in the error:
    includes fields that are not serected by the query...

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I forgot to include that the feild is already stored in a date format (general date format)
    You may want to check that again. If the field is truly stored as a Date data type, the sorting would work correctly (it would sort be date).
    The way you have described it, it is being sorted as if it were a text field.

    However, in looking back at your original query, it looks like you are sorting by ProjectName first, and then your Date field. Maybe that is what is confusing you.
    Try just sorting by the date field, and see what it looks like (see code below). If it truly a date field, it won't sort like you have described in your original post.
    Code:
    SELECT 
    [tblChangeProjectList].[ProjectName], 
    [tblChangeProjectList].[FilePath], 
    [tblChangeProjectList].[FilePath], 
    [tblChangeProjectList].[TimesVisited], 
    [tblChangeProjectList].[LastTimeVisited]
    FROM tblChangeProjectList 
    ORDER BY [LastTimeVisited] DESC

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    SELECT [tblChangeProjectList].[ProjectName], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[TimesVisited], [tblChangeProjectList].[LastTimeVisited] FROM tblChangeProjectList ORDER BY [ProjectName] UNION Select "< Pick from List...>", "<PICK...>", "", "", "" FROM [tblChangeProjectList]
    ORDER BY DateValue([tblChangeProjectList].[LastTimeVisited]) DESC , [tblChangeProjectList].[TimesVisited];

    which results in the error:
    includes fields that are not serected by the query...
    What exactly are you trying to do with the "UNION" part?
    Try removing it for now, to get the rest working properly. When you have that part working well, then we can talk about what you are trying to do there.

  8. #8
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    checked... it's definely a date
    also deleted the order by project name

    still...

    SELECT [tblChangeProjectList].[ProjectName], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[TimesVisited], [tblChangeProjectList].[LastTimeVisited] FROM tblChangeProjectList UNION Select "< Pick from List...>", "<PICK...>", "", "", "" FROM [tblChangeProjectList]
    ORDER BY [tblChangeProjectList].[LastTimeVisited] DESC , [tblChangeProjectList].[TimesVisited];

  9. #9
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    the union part allow me to add the value <pick from list) for those cases when the value isn't already stored in the table [tblChangeProjectList] (which is simple a list of any of the projects that they have visited in the last 30days...), the user has the option of going to another loctation to pick a value

  10. #10
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    As I suggested in the previous post, try it without the UNION query first.
    Code:
    SELECT 
    [tblChangeProjectList].[ProjectName], 
    [tblChangeProjectList].[FilePath], 
    [tblChangeProjectList].[TimesVisited], 
    [tblChangeProjectList].[LastTimeVisited] 
    FROM tblChangeProjectList 
    ORDER BY 
    [tblChangeProjectList].[LastTimeVisited] DESC, 
    [tblChangeProjectList].[TimesVisited];
    When you run into these type of situations, simplify first, get it working correctly, and then build upon that.

  11. #11
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    deleted the union clause, and it worked:

    SELECT
    [tblChangeProjectList].[ProjectName],
    [tblChangeProjectList].[FilePath],
    [tblChangeProjectList].[FilePath],
    [tblChangeProjectList].[TimesVisited],
    [tblChangeProjectList].[LastTimeVisited]
    FROM tblChangeProjectList
    ORDER BY LastTimeVisited DESC , [tblChangeProjectList].[TimesVisited];

  12. #12
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    could the problem be that the correlaing feild in the union statement is: "" -which is a text value, at it is being unioned to a"date" feild which is the sort...

    so the field consists of both date and text - which is throwing the sort into a tizzy
    the question is then: how do i add a 'fake' date value to the union...

  13. #13
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Yes, I think that is exactly what is going on. You need to UNION fields of the same data type, or else it will probably convert them to text.

    So I would recommend adding some dummy date field in there, i.e.
    Code:
    ...UNION Select "< Pick from List...>", "<PICK...>", "", "", DateSerial(2099,12,31)...

  14. #14
    markjkubicki's Avatar
    markjkubicki is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Location
    Fire Island Pines, NY
    Posts
    496
    BANG... that was it!!!
    big thnx

    SELECT [tblChangeProjectList].[ProjectName], [tblChangeProjectList].[FilePath], [tblChangeProjectList].[TimesVisited], [tblChangeProjectList].[LastTimeVisited] FROM tblChangeProjectList ORDER BY [ProjectName] UNION Select "< Pick from List...>", "<PICK...>", 1, DateSerial(2099,12,31) FROM [tblChangeProjectList] ORDER BY [tblChangeProjectList].[LastTimeVisited] DESC , [tblChangeProjectList].[TimesVisited];

  15. #15
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome. Glad we got it all working!

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

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2014, 03:59 AM
  2. Change Sort Order of Combo Box "On-The-Fly"
    By JoeM in forum Programming
    Replies: 6
    Last Post: 09-26-2013, 06:50 PM
  3. Replies: 1
    Last Post: 12-28-2012, 02:54 PM
  4. Replies: 2
    Last Post: 09-29-2012, 11:22 PM
  5. Replies: 11
    Last Post: 03-15-2012, 01:36 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