functions with parameters

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

Guest

I cannot get the following function to work:

Function EarlySuccess(HatchSuccess, FledgeSuccess,FledgeSurvive) As Single
HatchSuccess = 1.5
FledgeSuccess = 0.78
FledgeSurvive = 0.43
EarlySuccess = HatchSuccess * FledgeSuccess * FledgeSurvive
End Function

I keep getting a compile error "Argument not optional". It also does not
matter if I define the parameters as Single within the Function ( ), or
as lines before the values are inputted.

I don't know what I am doing wrong.

Thanks,

LAF
 
I cannot get the following function to work:

Function EarlySuccess(HatchSuccess, FledgeSuccess,FledgeSurvive) As Single
HatchSuccess = 1.5
FledgeSuccess = 0.78
FledgeSurvive = 0.43
EarlySuccess = HatchSuccess * FledgeSuccess * FledgeSurvive
End Function

I keep getting a compile error "Argument not optional". It also does not
matter if I define the parameters as Single within the Function ( ), or
as lines before the values are inputted.

I don't know what I am doing wrong.

Thanks,

LAF

Try:

Public Function EarlySuccess(VarA, VarB,VarC) As Single
EarlySuccess = VarA * VarB * VarC
End Function

Then send the variable data to the function from a query or form or
report using....
from a query:
NewField:EarlySuccess([FieldHatch],[FieldSuccess],[FieldSurvive])

or from a form or report unbound control:
=EarlySuccess([FieldHatch],[FieldSuccess],[FieldSurvive])

Use your actual field names when you send the data to the function,
but leave the VarA,VarB, and VarC as is in the function.

It would be simpler to just use an expression within the query or
form/report itself:

In a query:
NewField:[FieldHatch] * [FieldSuccess] * [FieldSurvive]

or in a form or report:
=[FieldHatch] * [FieldSuccess] * [FieldSurvive]

Use your actual field names.
 
Thanks, Fred, but I still do not see why I got the error when I defined the
value of the parameters within the function. At this stage of the program, I
just want to get the function to operate with given values. Where those
values come from will be a future task. So now that question I have is more
focussed: Can one specify the value of the parameters within the body of the
function?

Thanks,

LAF

fredg said:
I cannot get the following function to work:

Function EarlySuccess(HatchSuccess, FledgeSuccess,FledgeSurvive) As Single
HatchSuccess = 1.5
FledgeSuccess = 0.78
FledgeSurvive = 0.43
EarlySuccess = HatchSuccess * FledgeSuccess * FledgeSurvive
End Function

I keep getting a compile error "Argument not optional". It also does not
matter if I define the parameters as Single within the Function ( ), or
as lines before the values are inputted.

I don't know what I am doing wrong.

Thanks,

LAF

Try:

Public Function EarlySuccess(VarA, VarB,VarC) As Single
EarlySuccess = VarA * VarB * VarC
End Function

Then send the variable data to the function from a query or form or
report using....
from a query:
NewField:EarlySuccess([FieldHatch],[FieldSuccess],[FieldSurvive])

or from a form or report unbound control:
=EarlySuccess([FieldHatch],[FieldSuccess],[FieldSurvive])

Use your actual field names when you send the data to the function,
but leave the VarA,VarB, and VarC as is in the function.

It would be simpler to just use an expression within the query or
form/report itself:

In a query:
NewField:[FieldHatch] * [FieldSuccess] * [FieldSurvive]

or in a form or report:
=[FieldHatch] * [FieldSuccess] * [FieldSurvive]

Use your actual field names.
 
Thanks, Fred, but I still do not see why I got the error when I defined the
value of the parameters within the function. At this stage of the program, I
just want to get the function to operate with given values. Where those
values come from will be a future task. So now that question I have is more
focussed: Can one specify the value of the parameters within the body of the
function?

Thanks,

LAF

fredg said:
I cannot get the following function to work:

Function EarlySuccess(HatchSuccess, FledgeSuccess,FledgeSurvive) As Single
HatchSuccess = 1.5
FledgeSuccess = 0.78
FledgeSurvive = 0.43
EarlySuccess = HatchSuccess * FledgeSuccess * FledgeSurvive
End Function

I keep getting a compile error "Argument not optional". It also does not
matter if I define the parameters as Single within the Function ( ), or
as lines before the values are inputted.

I don't know what I am doing wrong.

Thanks,

LAF

Try:

Public Function EarlySuccess(VarA, VarB,VarC) As Single
EarlySuccess = VarA * VarB * VarC
End Function

Then send the variable data to the function from a query or form or
report using....
from a query:
NewField:EarlySuccess([FieldHatch],[FieldSuccess],[FieldSurvive])

or from a form or report unbound control:
=EarlySuccess([FieldHatch],[FieldSuccess],[FieldSurvive])

Use your actual field names when you send the data to the function,
but leave the VarA,VarB, and VarC as is in the function.

It would be simpler to just use an expression within the query or
form/report itself:

In a query:
NewField:[FieldHatch] * [FieldSuccess] * [FieldSurvive]

or in a form or report:
=[FieldHatch] * [FieldSuccess] * [FieldSurvive]

Use your actual field names.

Function EarlySuccess() As Single
Dim VarA as Single
Dim VarB as Single
Dim VarC as Single
VarA = 1.5
VarB = 0.78
VarC= 0.43
EarlySuccess = sngA * sngB * sngC
End Function
 
Based on reading through this thread, the problem probably is that when you
were testing this, you did not pass it any arguments. It would work if you
passed it:
x= EarlySuccess(2, .99, .55)
Unless you specifiy an argument as Optional for a function, you get the
error you encounter. Just go into VBA Help and read up on Funtion. It will
explain it for you.
Yes, you can change the value of the parameter that was passed.
 
Back
Top