Variable changing from value when calling function.

F

Frank Dulk

I am creating an application to calculate the loan of a value with monthly
payments and interests. I know that the function exists PMT that makes that,
but I need one that calculates considering the values for useful days and to
inform the date for better payment.

Already ta everything "almost" ready, until it works but it happens the
following mistake.

I created a function to verify the day it is not a bank holiday, if it goes,
it shows the first useful day. This function is perfect.

The function that calculates the value of the portion is the following:

Pagamento(Valor Currency, Portions Integer, Interest, PrimeiroPagamento
Dates Them) Currency

If I define the date of first payment for 01/11/03, I call the function:

ProximoDiaUtil(primeiropagamento)

The function sees that 01/11 are not useful day and it defines the date for
03/11/03 that it is one Monday.

However, starting from the moment that I use that function, the variable
PrimeiroPagamento that before was 01/11/03 become it fastens 03/11/03 and
all the other references are used starting from this date.

The payments instead of they be like this, 03/11/03, 01/12/03 and 02/01/04
are all for the 3 of every month.

I don't use in any part of the code
PrimeiroPagamento=ProximoDiaUtil(PrimeiroPagamento), but after the first it
GOES, Access understands as if I had defined.

This is the function that calculates the interest

Public Function Pagamento(Valor As Currency, Parcelas As Integer, Juro,
ByRef PrimeiroPagamento As Date) As Currency
ReDim Dias(Parcelas) As Integer
ReDim Vencimentos(Parcelas)
Dim ParcelaLiquida As Currency
Dim ValorParcela As Currency
Dim i As Integer

ParcelaLiquida = Valor / Parcelas
' Here the variable is 01/11/2003
For i = 2 To Parcelas
Dias(1) = DateDiff("d", Date, ProximoDiaUtil(PrimeiroPagamento))
Dias(i) = DateDiff("d", Date, ProximoDiaUtil(DateAdd("m", i - 1,
PrimeiroPagamento)))
A partir daqui a variável fica fica com valor 03/11/2003
Vencimentos(1) = ProximoDiaUtil(PrimeiroPagamento)
Vencimentos(i) = DateAdd("m", i - 1, PrimeiroPagamento)
Next

For i = 1 To Parcelas
ValorParcela = ValorParcela + ParcelaLiquida * Dias(i) * Juro / 3000
Next

Pagamento = (ValorParcela + Valor) / Parcelas

' It shows the values in the Box of Listing
Forms!Formulário1!Lista56.RowSource = "Vencimento;Valor;Dias"

For i = 1 To Parcelas
Forms!Formulário1!Lista56.RowSource = Forms!Formulário1!Lista56.RowSource +
";" & Format(Vencimentos(i), "dd/mm/yy") & ";" & Format((ValorParcela +
Valor) / Parcelas, "Currency") & ";" & CStr(Dias(i))
Next

End Function
 
M

Matthew Sullivan

Frank:

Your problem is (likely) due to the fact that (some) variables can be
passed one of two ways: "By Reference", or "By Value".

[I say "likely" due, because I didn't see where you posted the code
for the function that is causing you the problem: ProximoDiaUtil.]

I'll let you read up in the Help and archives about the difference
between "Reference" and "Value", because this is a subject you
*really, really* need to understand well.

Two possible ways to fix the problem:

1) Change your declaration of ProximoDiaUtil to look like this (note
the "ByValue"):
Public Function ProximoDiaUtil(ByValue primeiropagamento As Date) As
Date

2) Within that function, declare a "working" or "temp" Date
variable. Set it equal to the parameter value, and then do all your
operations and calculations with that working variable.

-Matt
 
F

Frank Dulk

See what answered me in another group:

Create a variable of the type it dates inside of the function, pass for this
variable the value that comes in the header, for the first portion, use this
date to see if it is useful day or not, for the following portions to use
the date of the header.

The problem is not in ByVal or ByRef. You are using the variable of the
header of the function as calculation variable.

What do you find?

Matthew Sullivan said:
Frank:

Your problem is (likely) due to the fact that (some) variables can be
passed one of two ways: "By Reference", or "By Value".

[I say "likely" due, because I didn't see where you posted the code
for the function that is causing you the problem: ProximoDiaUtil.]

I'll let you read up in the Help and archives about the difference
between "Reference" and "Value", because this is a subject you
*really, really* need to understand well.

Two possible ways to fix the problem:

1) Change your declaration of ProximoDiaUtil to look like this (note
the "ByValue"):
Public Function ProximoDiaUtil(ByValue primeiropagamento As Date) As
Date

2) Within that function, declare a "working" or "temp" Date
variable. Set it equal to the parameter value, and then do all your
operations and calculations with that working variable.

-Matt



If I define the date of first payment for 01/11/03, I call the function:

ProximoDiaUtil(primeiropagamento)

The function sees that 01/11 are not useful day and it defines the date for
03/11/03 that it is one Monday.

However, starting from the moment that I use that function, the variable
PrimeiroPagamento that before was 01/11/03 become it fastens 03/11/03 and
all the other references are used starting from this date.
 
M

Matthew Sullivan

Frank:

That answer agrees with the 2nd solution I gave above: set a local
variable equal to the parameter and then do your operations on that
local variable.

It looks like you're getting conflicting info about whether the ByVal
thing can solve your problem. Why not just try using ByVal and see
what happens?

-Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top