Function Parameters

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

Guest

I am passing several parameters to a function I have created, but I need to
get back 2 parameter values - how do I do it and how do I reference it? eg.

Exp1: breach (a,b,c)

breach will = the return value, but how to get a second value back?
 
Public Function TwoValues(ByVal a As Long, ByVal b As Long, _
ByVal c As Long, ByRef d As Long)

TwoValues = a + b + c
d = a - b - c

End Function

Public Sub TestTwoValues()

Dim d As Long
Debug.Print TwoValues(10, 20, 30, d)
Debug.Print d

End Sub
 
While Brendan & Chris have given you correct information about using
parameters with functions, I think they missed the fact that you appear to
want to be able to do this in a query. You can't!

You'll have to create 2 functions, one that returns the first value, and
another that returns the second value. Actually, for maintainability, it's
probably best to have your single function that passes backe parameter
values, and have little "wrapper" functions that call that function and each
return one specific value.
 
Douglas J. Steele said:
While Brendan & Chris have given you correct information about using
parameters with functions, I think they missed the fact that you appear to
want to be able to do this in a query. You can't!

You'll have to create 2 functions, one that returns the first value, and
another that returns the second value. Actually, for maintainability, it's
probably best to have your single function that passes backe parameter
values, and have little "wrapper" functions that call that function and
each return one specific value.

Can you create a user-defined type with two elements and make the function
of that type?

Tom Lake
 
Paul Dennis said:
I am passing several parameters to a function I have created, but I need to
get back 2 parameter values - how do I do it and how do I reference it? eg.

Exp1: breach (a,b,c)

breach will = the return value, but how to get a second value
back?

Paul Dennis,

The following is an example that illustrates the passing of user
defined types in VBA.

Create a new module (temporary, named whatever you like).

At the top, in the declarations, place:

Public Type SystemLogin

FName As String
LName As String
UserID As String
Password As String

End Type


Then add:

Private Function TypePassing _
(FName As String _
, LName As String _
, UserID As String _
, Password As String) As SystemLogin

Dim NewUser As SystemLogin

NewUser.FName = FName
NewUser.LName = LName
NewUser.UserID = UserID
NewUser.Password = Password

TypePassing = NewUser

End Function


Public Sub SetNewUser()

Dim NewUser As SystemLogin

NewUser = TypePassing("Jay", "Smith", "Marco", "Polo")

With NewUser
Debug.Print .FName
Debug.Print .LName
Debug.Print .UserID
Debug.Print .Password
End With

End Sub

Run SetNewUser.

The results (in the Immediate Window):

Jay
Smith
Marco
Polo


Sincerely,

Chris O.
 
Tom Lake said:
Can you create a user-defined type with two elements and make the function
of that type?

You could, Tom, but the problem is that a JET query (unlike a VBA procedure)
doesn't 'know' anything about UDTs.

Doug is right, the fact that the result is needed in a query changes things
significantly.
 
Douglas J. Steele said:
While Brendan & Chris have given you correct information about using
parameters with functions, I think they missed the fact that you appear to
want to be able to do this in a query. You can't!

Oops. :O

"Expr1:" seems pretty clear, in hindsight.


Sincerely,

Chris O.
 
Back
Top