Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning multiple values from a function

Status
Not open for further replies.

demoman

Programmer
Oct 24, 2001
242
0
0
US
Howdy

I have a function which accepts city and county code arguments, and returns the taxrate. The function needs to search the rates table and find the taxrate code, and rate.
I would like to be able to return both values to the calling program, not just the rate.

I have never used multiple returned values, and cannot find any examples. Perhaps, it is poor practice or not allowed?

As always, I appreciate any insight!
 
A function can only return one value, but you can pass as many parameters, of any type, as you need to By Reference, and essentially accomplish the same thing. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 

You can pass back a UDT (User Defined Type) or as one of the arguements you could pass in a variable byref i.e.

Option Explicit

Private Sub Command1_Click()

Dim I As Integer
I = 0
Test I

MsgBox I

End Sub

Private Sub Test(ByRef myval As Integer)
myval = 1
End Sub

 
In a .bas module stick this:
Code:
Type myTax
    taxcode As Integer
    taxrate As String
End Type
    
Public Function GetTax(myCounty As String, myCity As String) As myTax
    ' do what you need
    ' to get data
    GetTax.taxcode = lookeduptaxcode
    GetTax.taxrate = lookeduptaxrate
End Function

In your form use this:
Code:
Dim TaxAnswer as MyTax
TaxAnswer = GetTax(county, city)
MsgBox "TaxCode = " & TaxAnswer.taxcode
MsgBox "TaxRate = " & TaxAnswer.taxrate
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Or, if there are really alot of values to return, have the function return a Variant array. ][/b]][/i]][/u]*******************************************************
[sub]General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Why don't you just put both values together in a string separated by a comma and then separate the values after the function has returned them using the "Instr" function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top