Home All Groups Group Topic Archive Search About

Print between two dates

Author
24 Nov 2007 4:44 PM
Robert
I am using VB6.0 SP5

I am using the following code in a DLL.

Public Function CountTotalYearPayments(Optional StMnt As String, Optional
EndMnt As String, Optional Yr As String, Optional CompID As String, Optional
VendorID As String) As Object
    'Find Total Payments Made.

    Dim strSQL As String

    On Error GoTo SelectErr

    strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo Between
'" & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & Yr & "' AND CompID = '" &
CompID & "' AND VendorID = '" & VendorID & "'"

    Set CountTotalYearPayments = cnBills.Execute(strSQL)

    Exit Function

SelectErr:
    MsgBox Err.Number & " - " & Err.Description
    CountTotalYearPayments = Err
    Err.Clear

End Function

This works as I need.

Question.
When I try to find the above, but the year is 2 different years, it does not
work.
ex:
strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo Between '"
& StMnt & "' AND '" & EndMnt & "' AND Yr = '" & StYr & "'  AND '" & EndYr &
"' AND CompID = '" & CompID & "' AND VendorID = '" & VendorID & "'"

How can I retreive the data from two different years

--
Thanks in advance
bob
rober***@mountaincable.net

Author
24 Nov 2007 5:28 PM
Bob Butler
"Robert" <rober***@mountaincable.net> wrote in message
news:%235F8XlrLIHA.5400@TK2MSFTNGP04.phx.gbl...
<cut>
> strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo Between
> '" & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & StYr & "'  AND '" &
> EndYr & "' AND CompID = '" & CompID & "' AND VendorID = '" & VendorID &
> "'"

try something like

....where ((yr=" & styr & " and monthno>=" & stmnt & _
") or (yr=" & endyr & " and monthno<=" & endmnt & ")) and compid='" & ....

Note that I'm assuming the Yr and MonthNo columns are numeric so I removed
the ' delimiters to eliminate the unecessary conversions.  If they are
character fields then you'll want to add those back.
Author
25 Nov 2007 12:05 AM
argusy
Bob Butler wrote:
Show quote
> "Robert" <rober***@mountaincable.net> wrote in message
> news:%235F8XlrLIHA.5400@TK2MSFTNGP04.phx.gbl...
> <cut>
>> strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo
>> Between '" & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & StYr & "' 
>> AND '" & EndYr & "' AND CompID = '" & CompID & "' AND VendorID = '" &
>> VendorID & "'"
>
> try something like
>
> ...where ((yr = " & styr & " and monthno >=" & stmnt & _
> ") or (yr=" & endyr & " and monthno <=" & endmnt & ")) and compid='" & ....
>
> Note that I'm assuming the Yr and MonthNo columns are numeric so I
> removed the ' delimiters to eliminate the unecessary conversions.  If
> they are character fields then you'll want to add those back.
>
>
Bob

where
(yr = styr and monthno >= stmnth) OR
(yr = endyr and monthno <= endmnt)

doesn't quite gel with me.
Something to do with years being on the right side as well

Thinking about it

Graham
Author
25 Nov 2007 4:04 AM
Bob Butler
"argusy" <arg***@slmember.on.net> wrote in message
news:13khf35r1pbe86d@corp.supernews.com...
> where
> (yr = styr and monthno >= stmnth) OR
> (yr = endyr and monthno <= endmnt)
>
> doesn't quite gel with me.
> Something to do with years being on the right side as well

Not sure I understand the issue...

assuming styr=2006, stmnt=6, endyr=2007, endmnt=5 (my first reply had
'stmnth' instead of 'stmnt' because those variables names I find very hard
to read) it becomes

((yr=2006 and monthno>=6) or (yr=2007 and monthno<=5))
Author
25 Nov 2007 6:20 AM
argusy
Bob Butler wrote:
Show quote
> "argusy" <arg***@slmember.on.net> wrote in message
> news:13khf35r1pbe86d@corp.supernews.com...
>> where
>> (yr = styr and monthno >= stmnth) OR
>> (yr = endyr and monthno <= endmnt)
>>
>> doesn't quite gel with me.
>> Something to do with years being on the right side as well
>
> Not sure I understand the issue...
>
> assuming styr=2006, stmnt=6, endyr=2007, endmnt=5 (my first reply had
> 'stmnth' instead of 'stmnt' because those variables names I find very
> hard to read) it becomes
>
> ((yr=2006 and monthno>=6) or (yr=2007 and monthno<=5))
>
s'OK.
I was trying to get the "between" dates.
The 'or' threw me, until I thought it through.
Works for one year
What happens with July 2004 through to Jun 2006 ?
I see Jul to Dec 2004, and Jan to Jun, 2006
2005 ?
Graham
Author
25 Nov 2007 2:38 PM
Bob Butler
Show quote
"argusy" <arg***@slmember.on.net> wrote in message
news:13ki522rak92m8b@corp.supernews.com...
> Bob Butler wrote:
>> "argusy" <arg***@slmember.on.net> wrote in message
>> news:13khf35r1pbe86d@corp.supernews.com...
>>> where
>>> (yr = styr and monthno >= stmnth) OR
>>> (yr = endyr and monthno <= endmnt)
>>>
>>> doesn't quite gel with me.
>>> Something to do with years being on the right side as well
>>
>> Not sure I understand the issue...
>>
>> assuming styr=2006, stmnt=6, endyr=2007, endmnt=5 (my first reply had
>> 'stmnth' instead of 'stmnt' because those variables names I find very
>> hard to read) it becomes
>>
>> ((yr=2006 and monthno>=6) or (yr=2007 and monthno<=5))
>>
> s'OK.
> I was trying to get the "between" dates.
> The 'or' threw me, until I thought it through.
> Works for one year
> What happens with July 2004 through to Jun 2006 ?
> I see Jul to Dec 2004, and Jan to Jun, 2006
> 2005 ?


That case isn't covered; you could get it by adding
... or (yr>styr and yr<endyr)
Author
25 Nov 2007 8:42 PM
argusy
Bob Butler wrote:
Show quote
> "argusy" <arg***@slmember.on.net> wrote in message
> news:13ki522rak92m8b@corp.supernews.com...
>> Bob Butler wrote:
>>> "argusy" <arg***@slmember.on.net> wrote in message
>>> news:13khf35r1pbe86d@corp.supernews.com...
>>>> where
>>>> (yr = styr and monthno >= stmnth) OR
>>>> (yr = endyr and monthno <= endmnt)
>>>>
>>>> doesn't quite gel with me.
>>>> Something to do with years being on the right side as well
>>>
>>> Not sure I understand the issue...
>>>
>>> assuming styr=2006, stmnt=6, endyr=2007, endmnt=5 (my first reply had
>>> 'stmnth' instead of 'stmnt' because those variables names I find very
>>> hard to read) it becomes
>>>
>>> ((yr=2006 and monthno>=6) or (yr=2007 and monthno<=5))
>>>
>> s'OK.
>> I was trying to get the "between" dates.
>> The 'or' threw me, until I thought it through.
>> Works for one year
>> What happens with July 2004 through to Jun 2006 ?
>> I see Jul to Dec 2004, and Jan to Jun, 2006
>> 2005 ?
>
>
> That case isn't covered; you could get it by adding
> .. or (yr>styr and yr<endyr)
>
Yep. But then I re-read the OP's post.
Robert's just doing a total for one year, so your expression is just fine for
what he's after. (that 'or' - really could't see it at first)
Author
25 Nov 2007 3:05 PM
David Kerber
In article <#5F8XlrLIHA.5***@TK2MSFTNGP04.phx.gbl>, robert11
@mountaincable.net says...
Show quote
> I am using VB6.0 SP5
>
> I am using the following code in a DLL.
>
> Public Function CountTotalYearPayments(Optional StMnt As String, Optional
> EndMnt As String, Optional Yr As String, Optional CompID As String, Optional
> VendorID As String) As Object
>     'Find Total Payments Made.
>
>     Dim strSQL As String
>
>     On Error GoTo SelectErr
>
>     strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo Between
> '" & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & Yr & "' AND CompID = '" &
> CompID & "' AND VendorID = '" & VendorID & "'"
>
>     Set CountTotalYearPayments = cnBills.Execute(strSQL)
>
>     Exit Function
>
> SelectErr:
>     MsgBox Err.Number & " - " & Err.Description
>     CountTotalYearPayments = Err
>     Err.Clear
>
> End Function
>
> This works as I need.
>
> Question.
> When I try to find the above, but the year is 2 different years, it does not
> work.
> ex:
> strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo Between '"
> & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & StYr & "'  AND '" & EndYr &
> "' AND CompID = '" & CompID & "' AND VendorID = '" & VendorID & "'"
>
> How can I retreive the data from two different years

You've had several suggestions, but I'd structure it completely
differently from their suggestions, by combining the year and month into
one expression (assuming they're both numbers; if they're strings then
you need to do it a slightly differently, but the same principals apply: 

begindate = StYr * 100 + StMo
endDate = EndYr * 100 + EndMo

strsql = "select sum(amtpd) as amtpd from payments where
yr * 100 + monthno between" & begindate & " and " & enddate


--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
Author
27 Nov 2007 12:47 AM
Robert
Thanks to all that replied.

I may have not been to clear, but the following code gives me the data
between 2 dates.

strSQL = "SELECT * FROM Payments WHERE MonthNo BETWEEN '" & StMnt & "' AND
'" & EndMnt & "' AND Yr BETWEEN '" & StYr & "' AND '" & EnYr & "' AND CompID
= '" & CompID & "' AND VendorID = '" & VendorID & "' ORDER BY Yr DESC,
MonthNo DESC"

Show quote
"Robert" <rober***@mountaincable.net> wrote in message
news:%235F8XlrLIHA.5400@TK2MSFTNGP04.phx.gbl...
>I am using VB6.0 SP5
>
> I am using the following code in a DLL.
>
> Public Function CountTotalYearPayments(Optional StMnt As String, Optional
> EndMnt As String, Optional Yr As String, Optional CompID As String,
> Optional VendorID As String) As Object
>    'Find Total Payments Made.
>
>    Dim strSQL As String
>
>    On Error GoTo SelectErr
>
>    strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo
> Between '" & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & Yr & "' AND
> CompID = '" & CompID & "' AND VendorID = '" & VendorID & "'"
>
>    Set CountTotalYearPayments = cnBills.Execute(strSQL)
>
>    Exit Function
>
> SelectErr:
>    MsgBox Err.Number & " - " & Err.Description
>    CountTotalYearPayments = Err
>    Err.Clear
>
> End Function
>
> This works as I need.
>
> Question.
> When I try to find the above, but the year is 2 different years, it does
> not work.
> ex:
> strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments WHERE MonthNo Between
> '" & StMnt & "' AND '" & EndMnt & "' AND Yr = '" & StYr & "'  AND '" &
> EndYr & "' AND CompID = '" & CompID & "' AND VendorID = '" & VendorID &
> "'"
>
> How can I retreive the data from two different years
>
> --
> Thanks in advance
> bob
> rober***@mountaincable.net
>
Author
27 Nov 2007 1:09 AM
Bob Butler
"Robert" <rober***@mountaincable.net> wrote in message
news:%23%23FeR8IMIHA.280@TK2MSFTNGP03.phx.gbl...
> Thanks to all that replied.
>
> I may have not been to clear, but the following code gives me the data
> between 2 dates.
>
> strSQL = "SELECT * FROM Payments WHERE MonthNo BETWEEN '" & StMnt & "' AND
> '" & EndMnt & "' AND Yr BETWEEN '" & StYr & "' AND '" & EnYr & "' AND
> CompID = '" & CompID & "' AND VendorID = '" & VendorID & "' ORDER BY Yr
> DESC, MonthNo DESC"

I really doubt that is working as you think it is.

For example, if you want everything from 10/2006 to 2/2007 you'd be
requesting "month between 10 and 2" and "year between 2006 and 2007".  Even
if the month part doesn't mind the reversed order you won't get Jan 2007
because month 1 is not included.

You either have to use a more complex set of ranges or convert everything to
dates.  Samples of both approaches have been suggested.
Author
27 Nov 2007 1:56 AM
Robert
I tried your example, but I keep getting an error message.

"Yr is not defined in the Aggregate Expression"

strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments where ((yr = '" & StYr &
"' and monthno >= '" & StMnt & "') or (yr= '" & EnYr & "' and monthno <= '"
& EndMnt & "')) AND CompID = '" & CompID & "' AND VendorID = '" & VendorID &
"' ORDER BY Yr DESC, MonthNo DESC"

'------------------

Show quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:eM9cTJJMIHA.2268@TK2MSFTNGP02.phx.gbl...
> "Robert" <rober***@mountaincable.net> wrote in message
> news:%23%23FeR8IMIHA.280@TK2MSFTNGP03.phx.gbl...
>> Thanks to all that replied.
>>
>> I may have not been to clear, but the following code gives me the data
>> between 2 dates.
>>
>> strSQL = "SELECT * FROM Payments WHERE MonthNo BETWEEN '" & StMnt & "'
>> AND '" & EndMnt & "' AND Yr BETWEEN '" & StYr & "' AND '" & EnYr & "' AND
>> CompID = '" & CompID & "' AND VendorID = '" & VendorID & "' ORDER BY Yr
>> DESC, MonthNo DESC"
>
> I really doubt that is working as you think it is.
>
> For example, if you want everything from 10/2006 to 2/2007 you'd be
> requesting "month between 10 and 2" and "year between 2006 and 2007".
> Even if the month part doesn't mind the reversed order you won't get Jan
> 2007 because month 1 is not included.
>
> You either have to use a more complex set of ranges or convert everything
> to dates.  Samples of both approaches have been suggested.
>
>
Author
27 Nov 2007 3:39 AM
Bob Butler
"Robert" <rober***@mountaincable.net> wrote in message
news:ePAPBjJMIHA.1168@TK2MSFTNGP02.phx.gbl...
>I tried your example, but I keep getting an error message.
>
> "Yr is not defined in the Aggregate Expression"
>
> strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments where ((yr = '" & StYr
> & "' and monthno >= '" & StMnt & "') or (yr= '" & EnYr & "' and monthno <=
> '" & EndMnt & "')) AND CompID = '" & CompID & "' AND VendorID = '" &
> VendorID & "' ORDER BY Yr DESC, MonthNo DESC"

I don't see anything obvious;

What do you get when you do Debug.Print strSQL after that assignment?

Is this Access, SQL Server, Oracle, something else?

Are the columns in the table named 'Yr' and 'MonthNo'?

What are the data types in the table for Yr and MonthNo?
Author
27 Nov 2007 3:48 AM
Bob Butler
Show quote
"Bob Butler" <noway@nospam.ever> wrote in message
news:%23OEOBdKMIHA.4684@TK2MSFTNGP06.phx.gbl...
> "Robert" <rober***@mountaincable.net> wrote in message
> news:ePAPBjJMIHA.1168@TK2MSFTNGP02.phx.gbl...
>>I tried your example, but I keep getting an error message.
>>
>> "Yr is not defined in the Aggregate Expression"
>>
>> strSQL = "SELECT SUM(AmtPd) AS AmtPd FROM Payments where ((yr = '" & StYr
>> & "' and monthno >= '" & StMnt & "') or (yr= '" & EnYr & "' and monthno
>> <= '" & EndMnt & "')) AND CompID = '" & CompID & "' AND VendorID = '" &
>> VendorID & "' ORDER BY Yr DESC, MonthNo DESC"
>
> I don't see anything obvious;

On second reading I do... you are selecting just Sum(AmtPd) but then trying
to sort by Yr and MonthNo.  That 'order by' wasn't there before.

If you want totals by month you need to

Select Yr, MonthNo, Sum(AmtPd) As TotalPd ....

AddThis Social Bookmark Button