How to Pass Values/Varaibles between forms?

Posted on 2006-04-15
Last Modified: 2010-05-01
I have a VB6.0 app with a lot of interaction between forms.  I hate using global variables but it seesm like this is the only way to pass information from one form to another.  For example I am on frmClientMaint, which is used to maintain the general information (name, address, contact, phone etc..) about a client.  I have another form (frmClientItem) that is used to maintian a list of products which this client can order.  The user presses a button on frmClientMaint to present frmClientItem.  frmClientItem needs to know which client is being worked on so I place this value in a global variable prior to executing, like this:

gCurrClientId = wkClientId
frmClientItem.Show vbmodal

gCurrClientID is defined in Module 1 as 'Public gCurrClientID As Long'.  It seems like there must be a better way of doing this but I haven't uncovered it yet.

What techniques to the experts use?
Question by:mlcktmguy
    LVL 8

    Accepted Solution

    If you want to know IMMEDIATELY what they do, you can create a function on the second form like:

    public function MyFunc(byref Var)
    //Var will have been passed to here
    end function

    If you need it more permanently... then you can have the same thing only in MyFunc you would take
    that var and save it to a private variable, either way you do not need a public variable.

    So basically the workaround of a public variable is to call a function on the other form which is public
    and have that store the value in its own private variable.
    LVL 13

    Expert Comment

    'Declaration needed:
    Private sYourValHere As String

    'Property Get/Let (for retreiving value)
    Public Property Get YourValHere() As String
       YourValHere = sYourValHere
    End Property

    Public Property Let YourValHere(ByVal vNewValue As String)
       sYourValHere = vNewValue
    End Property

    Private Sub Form_Load()
       Form1.YourValHere = "test"
       MsgBox Form1.YourValHere
    End Sub
    LVL 1

    Author Comment

    Both solutions are valid and address my situation, hoever list244 was first so I will award the point to him.  Thatnks for your responses.
    LVL 13

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

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

    761 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

    8 Experts available now in Live!

    Get 1:1 Help Now