sub procedure that doesn’t always calculate properly.

theSizz

Registered User.
Local time
Today, 07:20
Joined
Nov 29, 2002
Messages
34
Can someone help me with a sub procedure that doesn’t always calculate properly.
I have a table named tblRecap that has daily profit and loss figures.
A query named qryMonthlySums that totals these daily figures for each month.
A form named frmGetReports where the user enters a year for a report to compile.
A report named rptVBATest that lists the sum of these monthly figures as profits or losses.
The code is suppose to enter a value in an unbound text control on the rptVBATest report that shows how many consecutive months that a profit was made and conversely how many consecutive months that a loss was made.

The code executes properly and gives the proper results for all years except
1993
2001
2004

Here is the code:
Private Sub ReportFooter_Format(Cancel As Integer, FormatCount As Integer)
Dim ConsqWin As Long
Dim ConsqLoss As Long
Dim tmpWin As Long
Dim tmpLoss As Long

'Note Yr is a number data type in the table
'and not a year data type.

Dim txtYr As Integer
Dim txtEvent As String

Dim Rs As DAO.Recordset
Dim db As DAO.Database
Dim qdf As DAO.QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qryMonthlySums")
qdf.Parameters(0) = [Forms]![frmGetReports]![Yr]



Set Rs = qdf.OpenRecordset

Do While Not Rs.EOF
Do While Rs!SumOfNet > 1
tmpWin = tmpWin + 1
Rs.MoveNext
If Rs.EOF Then Exit Do
Loop
If Rs.EOF Then Exit Do
If tmpWin > ConsqWin Then ConsqWin = tmpWin
tmpWin = 0
Rs.MoveNext
Loop

Rs.Close
qdf.Close

Set Rs = qdf.OpenRecordset

Do While Not Rs.EOF
Do While Rs!SumOfNet < 1
tmpLoss = tmpLoss + 1
Rs.MoveNext
If Rs.EOF Then Exit Do
Loop
If Rs.EOF Then Exit Do
If tmpLoss > ConsqLoss Then ConsqLoss = tmpLoss
tmpLoss = 0
Rs.MoveNext
Loop

Me.txtConsecMW.Value = ConsqWin
Me.txtConsecML.Value = ConsqLoss

Rs.Close
qdf.Close

Set Rs = Nothing
End Sub

I’ve attached the database and perhaps someone can review it and see what I am missing.
I have worked on this for 3 days and just don’t see me solving the issue. It’s probably something right in front of my face, but I can’t figure it out. Any help would be greatly appreciated.
 

Attachments

Report

Give this a shot


Dim ConsqWin As Long
Dim ConsqLoss As Long
Dim tmpWin As Long
Dim tmpLoss As Long

'Note Yr is a number data type in the table
'and not a year data type.

Dim txtYr As Integer
Dim txtEvent As String

Dim Rs As DAO.Recordset
Dim db As DAO.Database
Dim qdf As DAO.QueryDef

Set db = CurrentDb
Set qdf = db.QueryDefs("qryMonthlySums")
qdf.Parameters(0) = [Forms]![frmGetReports]![Yr]



Set Rs = qdf.OpenRecordset

Do While Not Rs.EOF

If Rs!SumOfNet > 1 Then
tmpWin = tmpWin + 1
tmpLoss = 0
End If

If Rs!SumOfNet < 1 Then
tmpLoss = tmpLoss + 1
tmpWin = 0
End If
If tmpLoss > ConsqLoss Then ConsqLoss = tmpLoss
If tmpWin > ConsqWin Then ConsqWin = tmpWin

Rs.MoveNext


Loop

Rs.Close
qdf.Close

Set Rs = qdf.OpenRecordset


Me.txtConsecMW.Value = ConsqWin
Me.txtConsecML.Value = ConsqLoss

Rs.Close
qdf.Close

Set Rs = Nothing
End Sub
 
Rick,
was there an answer in your reply or did I miss something ?
 
Report

Replace your code with the code in my reply in your report where your code is (On Format Report Footer).
 
OK Rick I see it.
I'll give it a try and let you know.
Thanks
 
OK Rick
I see where I went wrong. I placed your code in all the other reports and it all appears to be working fine.Thank you very very much.
 

Users who are viewing this thread

Back
Top Bottom