Solved

Returning several values from a procedure or function

Posted on 2002-06-19
7
166 Views
Last Modified: 2010-05-02
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
Comment
Question by:lslim
7 Comments
 
LVL 3

Accepted Solution

by:
nigelrowe earned 50 total points
ID: 7092176
How about...

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

by:Guy Hengel [angelIII / a3]
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

by:corvanderlinden
ID: 7092354
yet another one


Private Function ReturnValues() As Long()

and so on .....
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:bob_online
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

by:rspahitz
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

by:DanRollins
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

by:SpideyMod
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question