retaining variable values between subs

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

ive got a form which totals up different costs depending on whether they have
been selected or not. If they have been selected they are given the value of
the textbox otherwise they are given 0.
The values are stored as InternetBill, ParkingBill, RentBill etc. This is
typical code (which works for what its supposed to do)

<!---
Private Sub Invoice_Click()

If Me![Invoice].Value = True Then
Me![invoiceComment].Enabled = True
Me![InvoiceAmount].Enabled = True
InvoiceBill = Me![InvoiceAmount]
Else
Me![invoiceComment].Enabled = False
Me![InvoiceAmount].Enabled = False
InvoiceBill = "0"
End If
-->

I need to update the Total depending on the value put into any of these
variables.
something like this:
<!--
Private Sub depositAmount_AfterUpdate()

Me![Total] = DepositBill + InternetBill + InvoiceBill + Otherbill + _
ParkingBill + RentBill + TelephoneBill

End Sub
-->

Ive tried using static variables put into the top of the form module and its
giving me errors when i use the event procedure, putting it into the sub
doesnt seem to store the value between subs.

how would i go about doing this? (or is there an better method?)

with much thanks

Amit
 
There are two things you could try here. If the variable total needs to be
seen by all subs on the form then you can declare it at the module
level(before any subs) like so:

dim total as currency

or if you only need to use the variable in the depositamount_afterupdate()
procedure you could declare it in the procedure with static:

static total as currency

Both of these will maintain the value so long as the module is not reset.
You can not use the Static keyword at the module level, as any variable
declared outside of a procedure is by definition static.

Eric
 
Back
Top