|
-
Aug 3rd, 2005, 08:24 AM
#1
Thread Starter
Frenzied Member
[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.
-
Aug 3rd, 2005, 08:29 AM
#2
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
-
Aug 3rd, 2005, 08:42 AM
#3
Re: Access sql, return all dates
create a function like this:
VB Code:
Private Function AllDates(Dates() As Date, dStart As Date, dEnd As Date)
ReDim Dates(0)
For x = 0 To DateDiff("d", dStart, dEnd)
Dates(x) = DateAdd("d", x, dStart)
If x <> DateDiff("d", dStart, dEnd) Then
ReDim Preserve Dates(x + 1)
End If
Next
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"
-
Aug 3rd, 2005, 09:45 AM
#4
Thread Starter
Frenzied Member
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.
-
Aug 3rd, 2005, 10:30 AM
#5
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"
-
Aug 3rd, 2005, 10:36 AM
#6
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 10:40 AM
#7
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"
-
Aug 3rd, 2005, 10:41 AM
#8
Thread Starter
Frenzied Member
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.
-
Aug 3rd, 2005, 10:42 AM
#9
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 10:42 AM
#10
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"
-
Aug 3rd, 2005, 10:43 AM
#11
Thread Starter
Frenzied Member
Re: Access sql, return all dates
-
Aug 3rd, 2005, 10:43 AM
#12
Thread Starter
Frenzied Member
Re: Access sql, return all dates
-
Aug 3rd, 2005, 10:44 AM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 10:49 AM
#14
Thread Starter
Frenzied Member
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.
-
Aug 3rd, 2005, 10:49 AM
#15
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"
-
Aug 3rd, 2005, 10:58 AM
#16
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 11:02 AM
#17
Thread Starter
Frenzied Member
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
-
Aug 3rd, 2005, 11:10 AM
#18
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 11:14 AM
#19
Thread Starter
Frenzied Member
Re: Access sql, return all dates
Geoff's latest sql example
Which Example
-
Aug 3rd, 2005, 11:20 AM
#20
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 11:24 AM
#21
Thread Starter
Frenzied Member
Re: Access sql, return all dates
Leaving now. Will try it in the afternoon, Thanks all
-
Aug 3rd, 2005, 12:07 PM
#22
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"
-
Aug 3rd, 2005, 12:12 PM
#23
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"
-
Aug 3rd, 2005, 12:26 PM
#24
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 12:26 PM
#25
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:
oConn.Execute "CREATE TABLE tmpDates (Dates DATE)"
For x = 0 To 10
oConn.Execute "INSERT INTO tmpDates VALUES(#" & DateAdd("d", x, Date) & "#)"
Next
rs.Open "SELECT tmpDates.Dates, Sum(IIf(IsNull([Table2].[Amount]),0,[Table2].[AMount])) AS Amount " & _
"FROM tmpDates LEFT JOIN Table2 ON tmpDates.Dates = Table2.DateField " & _
"GROUP BY tmpDates.Dates", oConn
Do While Not rs.EOF
Debug.Print rs.Fields(0).Value & ": " & rs.Fields(1).Value
rs.MoveNext
Loop
rs.Close
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"
-
Aug 3rd, 2005, 12:28 PM
#26
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
-
Aug 3rd, 2005, 12:36 PM
#27
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 12:40 PM
#28
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"
-
Aug 3rd, 2005, 12:56 PM
#29
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 3rd, 2005, 06:12 PM
#30
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|