?
Solved

Passing Variables Between Subs

Posted on 2000-02-16
19
Medium Priority
?
248 Views
Last Modified: 2010-05-02
I have 2 sepatate forms.  When I call the 2nd form from the first, I want to pass a string variable to it.  I'm not sure where to define the variable to be accepted.  I've tried the form load, activate and initialize to no avail.  I just need to know where to define the variable

Thanks,
Brian
0
Comment
Question by:BrianMaslowski
[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
  • 4
  • 4
  • +4
19 Comments
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2527628
If the forms are in the same project, then declare the variable as Public in one of the form's General Declaration sections.  This will allow the variable to be global in scope.

:>)
0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2527638
Should have mentioned that a good practice is to have a module that contains global variables.  This allows sharing of variables between forms, classes and modules.  You should always declare global variables in the General Declarations section as follows:

Public g_strMyStringVariable as String

In doing this your entire project my now access this variable!

0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2527653
Oops, one last item.  If you decide to delcare the variable as global within one of the forms, say the form name is frmSecondForm and the variable name is strStringValue you will call it in the other form as follows:

frmSecondForm.strStringValue

Otherwise if you declare the variable in a module you only need to use:

strStringValue

Sorry for allow the comments, want to make sure I don't forget something!
:>)


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:Vbmaster
ID: 2527843
If you need to do something depending on what the value might be a better solution than using the Public varname As vartype is using a private variable and two public properties. Like this...

Private m_Something As String

Public Property Let Something(New_Value As String)

  m_Something = New_Value

End Property

Public Property Get Something() As String

  Something = m_Something

End Property


If you add this code to the form you can set the variable using code like...

  formname.Something = "Test"

This will call the Public Property Let Something (..) procedure.
0
 
LVL 3

Expert Comment

by:MTroutwine
ID: 2527914
I would accept VBMaster's solution.  This is the proper way to pass variables between objects in a project!
:>)
0
 
LVL 1

Expert Comment

by:rammeri
ID: 2527940
HeyHey ... I won't be that sure about this ...

There's another (IMHO better-to-understand) solution.

In Form2 (the called form) add the following

dim mParam as string

Public Sub ShowWithData(byval ThisString as string)
    mParam = thisString
    ' do some processing
    me.show
End Sub

In Form1 you do the following instead of the old Form2.Show

Sub command1_click()
    form2.showwithdata "Hello World!"
end sub
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2528124
Yet another choice.
Every form has an unused property called
Tag, which is available to you.
So, in form 1 you could say

Form2.Tag = "Hello Form 2"

And when you get to form 2 lo if you read Tag it will say Hello Form 2.

0
 

Expert Comment

by:TheFly
ID: 2528140
vbmaster solution follows the rules of variable encapsulation. a much better way of programming i think we will all agree.
0
 

Expert Comment

by:tcornett
ID: 2528174
Yes I agree with vbmaster also.  Using public properties like he described, not only do you follow the rules like TheFly stated, but you get  MUCH cleaner looking code, you get the ability to pass more than one var to the form (as opposed to using the "Tag" method of 3rsrichard), and when you go back to the project a month or two later, you can trace to flow of the program much easier than if you were to use global variables.  I just thought I'd back up vbmaster :).

- Tom
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2528232
Tag is encapsulated, Microsoft encapsulated it at the factory.

Brian is welcome to follow the ways of OOP, but why stop with m_Something .
Wheres his business case, his UML diagram.  
Brian doesn't need to be doing simple things just because he's a beginner,  methodology is a lot more important than results!!
0
 
LVL 1

Expert Comment

by:rammeri
ID: 2528343
I don't really agree with theFly (or vbmaster) as I think the encapsulation of my example is higher.

If you have f.e. a form that displays a list of invoices. Say you want to query them by dateFrom, dateTo and customer-number.

In vbMasters example you'd have to do

form1.datefrom = #1/1/2000#
form1.dateto = #1/10/2000#
form1.customernr = 1234
form1.show

whereas you could also do

form1.showList #1/1/2000#,#1/10/2000#,1234

if showlist is like
public sub showlist(byval dateFrom as date, byval dateTo as date, byval CustNo as long)


even better when you'd like to have the user select a value you could simply do it like:


(declarations of form)
dim mSelectedInvoiceNr as Long

public Function getInvoiceNr(byval dateFrom as date, byval dateTo as date, byval CustNo as long) as long
   ' initialize the grid with the data
   ' ....

   me.show vbmodal
   if mSelectedInvoiceNr = 0 then
       getInvoiceNr = 0
       unload me
       err.raise vbobjecterror + 123,,"No Invoice selected"
   else
        getInvoiceNr = mSelectedInvoiceNr
        unload me
   end if
end function

sub cmdSelect_Click()
    mSelected = <get the current number somehow>
    me.hide
end sub

sub cmdCancel_Click()
    mSelected = 0
    me.hide
end sub


you could then call it like

    myNr = form1.getInvoiceNr(...)


I think that this is even more encapsulated.

(this code wasn't checked, but it should show my point of view)


Cheers,
Ingo
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2528518
Are you guys trying to make Brians life miserable?
He didn't ask you how to build a computer, just where to put in the quarter to start it.  Can anyone come up with a method more convoluted?
0
 

Accepted Solution

by:
lenlutz earned 100 total points
ID: 2528680
Am i nuts or something...
This is VERY simple (unless i dont understand the question)
Heres a try:

FORM1

sub command1_click()
dim myStr as string

    myStr = "hello"
    form2.showit myStr
end sub



FORM2

sub showit(ms as string)
    text1.text = ms ' a test box on form 2 will show the string
end sub
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2528896
lenlutz: I know it may sound crazy but around here we (who's this we I keep hearing about?) use the answer feature when we have something to add. ;)
0
 
LVL 1

Expert Comment

by:rammeri
ID: 2534061
hmmm ... i think lenlutz just said what I did several lines above ;)
0
 

Expert Comment

by:lenlutz
ID: 2534864
yes rammeri.... you are correct.... (i must have missed it)

Whats this "use answer feature" ?

Please expalin that ?
thanks
0
 
LVL 1

Expert Comment

by:rammeri
ID: 2534934
lenlutz: he's talking about the "propose an answer"-option when posting a feedback (instead of using a comment) ;)

ingo
0
 

Expert Comment

by:lenlutz
ID: 2536558
at the bottom of the page,i see:



                       1.Have a comment for BrianMaslowski?

                       2.Or you could withdraw your proposed answer, and make it a comment

                         Comment  (with an option button)
                         Withdraw Answer   (with an option button)

i see nothing that looks like:
                         propose an answer
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2536640
lenlutz
It's hard to tell if you are being intentionally obtuse or not.
When you submitted your answer there were two button choices Answer or Comment.
VB was suggesting that since your answer was just a rehash of of what rammeri had already said it might be considered unfair to claim it was an answer.
Of the two choices you now see at the bottom of the page the one you have obviously found lets you make a further comment.  The one you haven't tried is the one you should choose.

Oh, and if you were being intentionally rude then you were much to subtle about it, nobody got the point.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

770 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