Solved

Passing Variables Between Subs

Posted on 2000-02-16
19
223 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 50 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now