Reset Public Variable Value to Zero (1 Viewer)

Dwight

Registered User.
Local time
Today, 16:22
Joined
Mar 17, 2003
Messages
168
I have a function that performs a running sum calculation. It has a public variable to store the value.

I call the function from a query to generate a running balance calculation. It works fine the first time. But if I close it and run the query again it is wrong because the calculation value begins where the last one left off.

The problem is because the previous amount remains leftover in my public variable. So I do I reset this variable to zero?

(yes, I know you can do running sums in reports but I would like to learn this programming technique).

Thanks in advance,

Dwight
 

Dwight

Registered User.
Local time
Today, 16:22
Joined
Mar 17, 2003
Messages
168
Thanks to Namlian for providing the answer..............he provided some code months ago that accomplished this but I guess I never fully understood what was going on. Now I do. Here is his method which I will use.

Does anyone have another technique?



Public LastTime as Date

If Timer - LastTime > 1 Then

MyAmount = 0 'If more than a second has elapsed the variable is reset to zero.

End If

LastTime = Timer
 

ted.martin

Registered User.
Local time
Today, 15:22
Joined
Sep 24, 2004
Messages
743
The obvious thing to do is just run the statement
MyPublicVar = 0 at the end of the function or when the function is called at the beginning.

It is always good practice to reset your variables to 0 of "" after use that way you can be sujre they are clean each time they are called.

Its all down to foresight in the database design and error-trapping.

Hope this helps.
 

Dwight

Registered User.
Local time
Today, 16:22
Joined
Mar 17, 2003
Messages
168
Ted:

Thanks for the reply. I am still fairly new to programming and "the obvious thing" is sometimes not obvious enough to me.

Here is my code:

Public BgnAmount As Currency
Public CurrencyCode As String

Public Function RunSum(crd As Currency, dbt As Currency, CurrencyID As String) As Currency

If Timer - LastTime > 1 Then

BgnAmount = 0 ' Resets the Variable
CurrencyCode = ""

LastTime = Timer

End If

If CurrencyCode = CurrencyID Then
RunSum = BgnAmount + crd + dbt
Else
BgnAmount = 0
CurrencyCode = CurrencyID
RunSum = crd + dbt
End If
BgnAmount = RunSum
End Function


Can you show me where to set the variables to zero because in the past when I have done it at the beginning or end of my function my running sum is affected.

Thanks.
 

jamesWP

Registered User.
Local time
Today, 16:22
Joined
Aug 8, 2005
Messages
68
Dwight said:
Ted:

Thanks for the reply. I am still fairly new to programming and "the obvious thing" is sometimes not obvious enough to me.

Here is my code:

Public BgnAmount As Currency
Public CurrencyCode As String

Public Function RunSum(crd As Currency, dbt As Currency, CurrencyID As String) As Currency

If Timer - LastTime > 1 Then

BgnAmount = 0 ' Resets the Variable
CurrencyCode = ""

LastTime = Timer

End If

If CurrencyCode = CurrencyID Then
RunSum = BgnAmount + crd + dbt
Else
BgnAmount = 0
CurrencyCode = CurrencyID
RunSum = crd + dbt
End If
BgnAmount = RunSum
End Function


Can you show me where to set the variables to zero because in the past when I have done it at the beginning or end of my function my running sum is affected.

Thanks.

Ok this should work, resets the variables every second.


Code:
Public BgnAmount As Currency
Public CurrencyCode As String

Public Function RunSum(crd As Currency, dbt As Currency, CurrencyID As String) As Currency

Me.TimerInterval = 1000
BgnAmount = 0 ' Resets the Variable
CurrencyCode = ""

If CurrencyCode = CurrencyID Then
  RunSum = BgnAmount + crd + dbt
Else
  BgnAmount = 0
  CurrencyCode = CurrencyID
  RunSum = crd + dbt
End If
  BgnAmount = RunSum
End Function
 
Last edited:

Users who are viewing this thread

Top Bottom