I would like to call a function that "returned" several values

I would like to call a function that "returned" several values

am 22.10.2007 16:08:41 von MLH

I would like to call a function that "returned" several values - all
of which are relevant to the needs of a procedure on a form. I do
understand that FN's return a single value.

I'm wondering, is this a good place to employ a user-defined Type?

The FN would reside in a global module. When called, it does return
one value considered to be the most important. But while it's running,
it does calculate several more important values that would otherwise
have to be REcalculated in the calling function when processing
returns there. Comments?

Re: I would like to call a function that "returned" several values

am 22.10.2007 16:36:13 von Allen Browne

The simplest approach is to pass some extra arguments *into* the function,
and set them in there. The calling routine can then check the value of those
variables afterwards.

A user-defined type is also a good solution for some cases. See:
Multiple return values from a Function - User-defined types
at:
http://allenbrowne.com/ser-16.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"MLH" wrote in message
news:1abph39j4u5p5002rerlhllbftdl3fhp3d@4ax.com...
>I would like to call a function that "returned" several values - all
> of which are relevant to the needs of a procedure on a form. I do
> understand that FN's return a single value.
>
> I'm wondering, is this a good place to employ a user-defined Type?
>
> The FN would reside in a global module. When called, it does return
> one value considered to be the most important. But while it's running,
> it does calculate several more important values that would otherwise
> have to be REcalculated in the calling function when processing
> returns there. Comments?

Re: I would like to call a function that "returned" several values

am 22.10.2007 17:03:53 von Lye Fairfield

MLH wrote in
news:1abph39j4u5p5002rerlhllbftdl3fhp3d@4ax.com:

> I would like to call a function that "returned" several values - all
> of which are relevant to the needs of a procedure on a form. I do
> understand that FN's return a single value.
>
> I'm wondering, is this a good place to employ a user-defined Type?
>
> The FN would reside in a global module. When called, it does return
> one value considered to be the most important. But while it's running,
> it does calculate several more important values that would otherwise
> have to be REcalculated in the calling function when processing
> returns there. Comments?

Public Function MyHeroes(ByVal p1, ByVal p2) As Variant
Dim a(6) As Variant
a(0) = UCase(p1)
a(1) = LCase(p1)
a(2) = StrConv(p1, vbProperCase)
a(3) = UCase(p2)
a(4) = LCase(p2)
a(5) = StrConv(p2, vbProperCase)
MyHeroes = a
End Function

Sub test()
Dim r As Variant
r = MyHeroes("giLbert a HIGHet", "harry s Truman")
Debug.Print r(0)
Debug.Print r(1)
Debug.Print r(2)
Debug.Print r(3)
Debug.Print r(4)
Debug.Print r(5)

'GILBERT A HIGHET
'gilbert a highet
'Gilbert A Highet
'HARRY S TRUMAN
'harry s truman
'Harry S Truman

End Sub

--
lyle fairfield

Re: I would like to call a function that "returned" several values

am 22.10.2007 18:48:38 von MLH

Thanks, Allen. In the simplest case, where only a single argument is
required for the function to do its job, there would be no need to
pass the FN additional info that would be superflous. But with that
single input, the FN may make a considerable number value assignments
along the way whilst it's doing its job. When done, it returns a
single and hopefully useful value to the calling procedure. What would
the addtional inputs do to reach the desired end?

>The simplest approach is to pass some extra arguments *into* the function,
>and set them in there. The calling routine can then check the value of those
>variables afterwards.

Re: I would like to call a function that "returned" several values

am 22.10.2007 18:56:43 von MLH

Thx, Lyle.

Yep. A 6-element array does the trick.
Of that, there's no doubt. And, it's clean
and easy to implement.

Now, just out-a-curiousity, under what
extenuating circumstances might you
elect to make use of a UDT as opposed
to the array solution you proposed? I'm
not assuming that you would - just wondering.

Re: I would like to call a function that "returned" several values

am 22.10.2007 19:05:13 von MLH

Oops. I might have err'd...
I assumed that a(6) was a variant array. But a small
addition to your code did not verify that...

Public Function MyHeroes(ByVal p1, ByVal p2) As Variant
Dim a(6) As Variant
a(0) = UCase(p1)
a(1) = LCase(p1)
a(2) = StrConv(p1, vbProperCase)
a(3) = UCase(p2)
a(4) = LCase(p2)
a(5) = StrConv(p2, vbProperCase)
MyHeroes = a
Debug.Print ".......", VarType(a), "......."

End Function

Sub test()
Dim r As Variant
r = MyHeroes("giLbert a HIGHet", "harry s Truman")
Debug.Print r(0)
Debug.Print r(1)
Debug.Print r(2)
Debug.Print r(3)
Debug.Print r(4)
Debug.Print r(5)

'....... 8204 .......
'GILBERT A HIGHET
'gilbert a highet
'Gilbert A Highet
'HARRY S TRUMAN
'harry s truman
'Harry S Truman

End Sub

I would have expected a value of 8192 - but that sure ain't
what I got. Hmmm???

Re: I would like to call a function that "returned" several values

am 22.10.2007 19:08:45 von MLH

Looks like its part vbVariant and vbArray
when evaluating the value returned in a
bitwise manner. Surely that's it. I mean, its
certainly not a UDT.

Re: I would like to call a function that "returned" several values

am 23.10.2007 02:02:43 von lyle

On Oct 22, 1:05 pm, MLH wrote:
> Oops. I might have err'd...
> I assumed that a(6) was a variant array. But a small
> addition to your code did not verify that...
>
> Public Function MyHeroes(ByVal p1, ByVal p2) As Variant
> Dim a(6) As Variant
> a(0) = UCase(p1)
> a(1) = LCase(p1)
> a(2) = StrConv(p1, vbProperCase)
> a(3) = UCase(p2)
> a(4) = LCase(p2)
> a(5) = StrConv(p2, vbProperCase)
> MyHeroes = a
> Debug.Print ".......", VarType(a), "......."
>
> End Function
>
> Sub test()
> Dim r As Variant
> r = MyHeroes("giLbert a HIGHet", "harry s Truman")
> Debug.Print r(0)
> Debug.Print r(1)
> Debug.Print r(2)
> Debug.Print r(3)
> Debug.Print r(4)
> Debug.Print r(5)
>
> '....... 8204 .......
> 'GILBERT A HIGHET
> 'gilbert a highet
> 'Gilbert A Highet
> 'HARRY S TRUMAN
> 'harry s truman
> 'Harry S Truman
>
> End Sub
>
> I would have expected a value of 8192 - but that sure ain't
> what I got. Hmmm???

TTBOMK 8192 is never returned from VarType.

One hopes that you got 8204.

8192 indicates an array; 12 indicates a variant. 8192 +12=8204,

TypeName(a) returns Variant(), that is, variant array.

Re: I would like to call a function that "returned" several values

am 23.10.2007 02:25:56 von lyle

On Oct 22, 12:56 pm, MLH wrote:
> Thx, Lyle.
>
> Yep. A 6-element array does the trick.
> Of that, there's no doubt. And, it's clean
> and easy to implement.
>
> Now, just out-a-curiousity, under what
> extenuating circumstances might you
> elect to make use of a UDT as opposed
> to the array solution you proposed? I'm
> not assuming that you would - just wondering.

If I wanted an excuse to go get a cup of coffee while my computer was
busy, I would use a UDT. No, I really don't know if that is fair, but
I do know that in most cases, arrays have a decided speed advantage
over other objects.
UDTs may be helpful in making code clear:
Person.LastName="McCabe"
Debug.Print Person.LastName
is more or less self documenting
while
Person(0)="McCabe"
Debug.Print Person(0)
may be less so.
And of course, multiple instances of UDTs are probably easier to deal
with than multiple instances of arrays
On the other hand, someone who means to pass UDTs to functions may
want to review the chapters on by value and by reference.

Re: I would like to call a function that "returned" several values

am 23.10.2007 02:26:36 von Allen Browne

You can make the arguments Optional, so you don't have to pass them all.

You can pass a ParamArray(), where the number of arguments will vary at
runtime.

You can pass a Recordset, which contains all the fields as values.

You can use a user-defined type, with a structure that's akin to a
recordset.

Horses for courses.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"MLH" wrote in message
news:7rkph3hepdrmc9ke6mm84nhsocbe77c7tn@4ax.com...
> Thanks, Allen. In the simplest case, where only a single argument is
> required for the function to do its job, there would be no need to
> pass the FN additional info that would be superflous. But with that
> single input, the FN may make a considerable number value assignments
> along the way whilst it's doing its job. When done, it returns a
> single and hopefully useful value to the calling procedure. What would
> the addtional inputs do to reach the desired end?
>
>>The simplest approach is to pass some extra arguments *into* the function,
>>and set them in there. The calling routine can then check the value of
>>those
>>variables afterwards.

Re: I would like to call a function that "returned" several values

am 23.10.2007 15:39:22 von MLH

OK. Many thx.

Re: I would like to call a function that "returned" several values

am 23.10.2007 15:41:01 von MLH

Gotcha. I later noticed the last paragraph
in HELP indicated exactly what you said.

Re: I would like to call a function that "returned" several values

am 23.10.2007 15:43:19 von MLH

OK. Thx.