Solved

Passing Variables

Posted on 2000-03-25
4
175 Views
Last Modified: 2010-08-05
I am still in the beginning stages of learning VB. Could you give a good explanatory example of passing variables and when to use 2 different names in the 1st procedure vs. the 2nd procedure?
0
Comment
Question by:Devlu
4 Comments
 
LVL 1

Expert Comment

by:ndb
ID: 2657316
Can you make your question a bit more clear please ...
0
 
LVL 3

Expert Comment

by:Foyal
ID: 2657582
Private Sub FirstSub(i As String, j As Integer)

    txtText.caption = i & " " & CStr(j)
   
    'i = S from SecondSub
    'j = k from SecondSub
   
End Sub

Private Sub SecondSub(k As Integer, S As String)
   
    k = 35
    S = "String came from Second Sub"
    txtText.Caption = ""
    FirstSub S,k
   

End Sub

The called subs varibles "inherit" the values of the variables sent to it... Make sure you get the types right and the order you send in correct.
0
 
LVL 2

Accepted Solution

by:
Chrism earned 35 total points
ID: 2661029
Devlu,

What you have asked is not straightforward.  The examples listed above show you the syntax to be able to write a function or sub and pass parameters to them.

Incidentally - to call these sub, just use:

call name_of_sub(param1,param2,etc)

where param1 (etc) is the name of the parameters you wish to pass.

The question about naming of parameters in the calling line and within the subroutine or function is a lot to do with personal preference.  If it is the same variable doing the same job, I tend to use the same name, but there is an entire school of thought that indicates you should always change the name within the sub or function.

The only real rule is: within each sub or function, you must use the same name. ie: the variable name you use is only defined within the context of that sub.

The questions you have not asked are:

Do these variables change value in the sub, and is this change passed back?

Can I change the variables in the sub, and not pass the change back?

The answers are: it depends how the params are passed, either ByRef or ByVal, and you can look these up for a better explanation of what each does.

A good place to start for all of this kind of stuff is:

http://vb-world.net/beginning/

Enjoy...
0
 

Author Comment

by:Devlu
ID: 2661482
Adjusted points from 25 to 35
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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