Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Returning several values from a procedure or function

Posted on 2002-06-19
Medium Priority
171 Views
hi,

I would like to get several return values from a sub or function. how can i do that because from my knowledge, i just understand that a function can return 1 value whilst we r not returning value(s) from a sub.

thanks.
0
Question by:lslim
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Accepted Solution

nigelrowe earned 200 total points
ID: 7092176

Private Sub Command1_Click()
Dim an_array As Variant
an_array = ReturnValues
End Sub

Private Function ReturnValues() As Variant
Dim i As Integer, s As String, o As Object
i = 1
s = "A string"
Set o = New Collection
ReturnValues = Array(i, s, o)
End Function
0

LVL 143

Expert Comment

ID: 7092226
Another way (if you don't need the flexibility of arrays of return values):

Private Function ReturnValues(byref i As Integer, byref s As String, byref o As Object ) As Long
on error goto Err
i = 1
s = "A string"
Set o = New Collection

'return 0 ->success
Exit function
Err:
ReturnValues = err.number

End Function

CHeers
0

LVL 2

Expert Comment

ID: 7092354
yet another one

Private Function ReturnValues() As Long()

and so on .....
0

LVL 5

Expert Comment

ID: 7092427
If you declare the arguements of a procedure ByRef, you can modify them in the sub (or function)

sub main()

dim St as string
dim Nu as integer

ST = "Going In"
NU = 9
Msgbox ST & " " & Nu

Modify_Us St, Nu

Msgbox ST & " " & Nu

End sub

Private Sub Modify_Us(ByRef St1 as string,ByRef nu1 as Integer)

St1 = "Coming Out"
Nu1 = 1

End Sub

You can modify any number of parameters this way without resorting to arrays or collecitons.
0

LVL 22

Expert Comment

ID: 7093346
If you want to maintain the concept of functions returning only a single thing, you can have it return a structure (i.e. object, user-defined type, array, or elementary variable)

The elementary will return a single value, which will only help if you make it a string and parse it on the other end.  This is not the best approach.

The array will return multiple items of the same type, and was listed as a choice in a previous comment.

The user-defined type lets you defined a collection of values to the referred to by a single name.

The object is essentially a user-defined type with more.

--
As indicated above, you can also break the one-value return by setting an "input" parameter as ByRef, after which any change to that valiable will impact the calling procedure also.

Also Microsoft uses this concept for some of its API calls, it is not recommended because it does not clearly indicate what will change.  Clarification: return values are expected to change; object properties are expected to change; input parameters should NOT change.
0

LVL 49

Expert Comment

ID: 7922706
Hi lslim,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

Split points between: nigelrowe and angelIII

lslim, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0

Expert Comment

ID: 7968504
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

angelIII points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20517678.html
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
###### Suggested Courses
Course of the Month9 days, 23 hours left to enroll