• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

How to Pass Values/Varaibles between forms?

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?
  • 2
1 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.
'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
mlcktmguyAuthor Commented:
Both solutions are valid and address my situation, hoever list244 was first so I will award the point to him.  Thatnks for your responses.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now