Solved

Passing Variables

Posted on 2000-03-25
4
178 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
[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
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 19 hours left to enroll

630 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