Results 1 to 30 of 30

Thread: [RESOLVED] Access sql, return all dates

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Resolved [RESOLVED] Access sql, return all dates

    I am using access and i would like to create a sql query that would return all dates.

    For example

    Select AllDate From Dates Where Date Betwwen #7/01/05# And #7/5/05#

    it should return

    7/01/05
    7/02/05
    7/03/05
    7/04/05
    7/05/05


    Without having to create a table Dates.

    Thanks for any help.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Access sql, return all dates

    If you don't have a table Dates whay would you want to query the database?

    Use a for loop or a Do Loop to generate the dates and store them in some sort of structure.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    create a function like this:
    VB Code:
    1. Private Function AllDates(Dates() As Date, dStart As Date, dEnd As Date)
    2. ReDim Dates(0)
    3. For x = 0 To DateDiff("d", dStart, dEnd)
    4.     Dates(x) = DateAdd("d", x, dStart)
    5.     If x <> DateDiff("d", dStart, dEnd) Then
    6.         ReDim Preserve Dates(x + 1)
    7.     End If
    8. Next
    9. End Function

    use like:
    Dim Dates() As Date
    AllDates Dates(), #8/1/2005#, #8/10/2005#

    now Dates contains 8/1 to 8/10
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    I figured out an alternative way. But i would still like to know if you can do it thru a query.

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    this may work.. but not for Access... this I think is PLSQL

    SELECT TRUNC(SYSDATE)||','||TRUNC(SYSDATE - 1)||','||TRUNC(SYSDATE - 2) FROM DUAL
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    You do have a field "AllDate" which contains dates? If so a small modification to your original query is all you need.
    VB Code:
    1. Select AllDate From Dates Where AllDate Between #7/01/05# And #7/5/05#

    Edit: Just seen that you dont want the table Dates. Hmm...
    Last edited by RobDog888; Aug 3rd, 2005 at 10:40 AM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    Rob.. there isnt even a TABLE with any dates...
    shragel is trying to return a range of dates from without having a table with dates in them
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    no. no fields and no table

    Just like doing

    select #7/01/05# from AnyTable

    Would return 7/01/05

    I want something that will return a range of dates.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    Ok, then what needs to happen is is a dynamic field alias in the sql statement.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    shragel is this Access ? PLSQL?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    how do you do that?

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    Access

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    But if there is no table then whats the point? You need to have a table or query to select from else this is not a query type issue.

    What exactly are you trying to do?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    Lets say i have a table with two fields Date, Amount

    I want to crerate a report with every date of a rang and the sum amount of that day. But i want every day to come up in the report not only days that an amount has been entered.

  15. #15
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    ok Trying this:
    SELECT #8/1/2005#,#8/2/2005#

    worked.. but it returns each Field as a date

    so if you did

    rs.open "SELECT #8/1/2005#,#8/2/2005#"

    then rs.fields(0).value = 8/1/2005
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    Even with that you would still need to add in the Amount field. What table is Amount coming from? Are you using Access reporting or CR? If CR you can use "Repeat page header on every page" option, or such. Then only need to worry about the amounts as the dates will be displayed on every page in a textbox.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    I would make it 0

    For example
    Select Mydate Sum(Amount) From MyTable Where Date between #7/01/05# AND #7/05/05# Group By MyDate

    will return

    7/01/05 50
    7/03/05 60
    7/04/05 110

    I want to get

    7/01/05 50
    7/02/05 0
    7/03/05 60
    7/04/05 110
    7/05/05 0

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    Ok, you could LEFT JOIN that query (without the MyDate) on to Geoff's latest sql example. The LEFT JOIN says to take everything from the first table and only join in the rows from the second table where the join is equal.

    Giving you:
    7/01/05 50
    7/02/05
    7/03/05 60
    7/04/05 110
    7/05/05
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    Geoff's latest sql example

    Which Example

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    "SELECT #8/1/2005#, #8/2/2005#, #8/3/2005#, #8/4/2005#, #8/5/2005#, Sum(Amount) FROM MyTable WHERE MyDate BETWEEN #8/1/2005# AND #8/5/2005# GROUP BY #8/1/2005#, #8/2/2005#, #8/3/2005#, #8/4/2005#, #8/5/2005#;"

    I think that may do it even easier.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    Leaving now. Will try it in the afternoon, Thanks all

  22. #22
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    Problem:

    that will return 1 Row
    Fields 0 - 4 will be a date, field 5 will be the sum...
    Im trying to figure out a way to have rows.. not cols with it
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  23. #23
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    wait.. if you dont have a date in the table.. the sum() will be the same in every row?

    this is getting confusing...
    where is amount coming from? another table?
    ah.. wait.. you need to return Days even if there is no data for that day!
    does the table with the amount have a date field? if so what is it?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    I think this issue is best solved in the report and not in the query. Which reporting are you using?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    ok .. best way is to create a temp table fill with dates..use for query then drop table

    VB Code:
    1. oConn.Execute "CREATE TABLE tmpDates (Dates DATE)"
    2. For x = 0 To 10
    3.     oConn.Execute "INSERT INTO tmpDates VALUES(#" & DateAdd("d", x, Date) & "#)"
    4. Next
    5. rs.Open "SELECT tmpDates.Dates, Sum(IIf(IsNull([Table2].[Amount]),0,[Table2].[AMount])) AS Amount " & _
    6.         "FROM tmpDates LEFT JOIN Table2 ON tmpDates.Dates = Table2.DateField " & _
    7.         "GROUP BY tmpDates.Dates", oConn
    8. Do While Not rs.EOF
    9.     Debug.Print rs.Fields(0).Value & ": " & rs.Fields(1).Value
    10.     rs.MoveNext
    11. Loop
    12. rs.Close
    13. oConn.Execute "DROP TABLE tmpDates"

    resulted in this:

    8/3/2005: 25
    8/4/2005: 0
    8/5/2005: 16
    8/6/2005: 0
    8/7/2005: 0
    8/8/2005: 0
    8/9/2005: 0
    8/10/2005: 0
    8/11/2005: 0
    8/12/2005: 0
    8/13/2005: 0
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  26. #26
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Access sql, return all dates

    Ok.... bottom line...there isn't a way to automagicaly do this in Access.... If this were SQL Serv, or something that allowed stored procs, then there's a solution.... but in this case there isn't a direct, do it all in one query, and make it work in Access solution. You have to have some kind of data source that can supply all the dates in a given range. That's where the displayed date is going to come from. It can't come from the table with the amount in it, because it simply does not exist. Once you have the date source, then you can left join it to the amounts table, using the date. Then and only then can you have contiguous, dates, with their related amounts for a given range, even when no amount exist.

    EDIT: I've just seen [LGS]Static's last post.... while not very elegant, it does get the job done.... or at least seems to... But one thing that was never answered was what kind of report is this? Access? simply data in a grid? Crystal? OTher?


    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    TG is right and verified my LEFT JOIN proposal (#18). But the poster doesnt want to have to create another table Geoff.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  28. #28
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Access sql, return all dates

    Rob,
    this:
    "SELECT #8/1/2005#, #8/2/2005#, #8/3/2005#, #8/4/2005#, #8/5/2005#, etc..

    will return:
    Field1.... Field2.... Field3... etc
    8/1/2005 8/2/2005 8/3/2005 8/4/2005 8/5/2005

    not
    8/1
    8/2
    8/3
    right?
    or am I looking at it wrong?
    It seemed to in my tests...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  29. #29
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access sql, return all dates

    Yes, we both made that mistake Fields and not Rows in one field.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  30. #30

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Access sql, return all dates

    Thanks All. It seems it cannot be done. I was ComponentOne VSReport. Its very similar to access reporting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width