How do I share a variable between forms?

OMRebel

Active member
Joined
Sep 27, 2006
Messages
44
Programming Experience
Beginner
I have a variable called intAccount. In one form, frmTrans, the intAccount is assigned its value based on a text book. A qry is run to see if that value is in a database, and if not, I show a new form, frmPatientEntry, without closeing the other form. I want to be able to fill in the value for a textbox I have on the new form from the intAccount.

How can this be done?
 
I figured it out. :eek:)

I had it setup as a Shared variable, but in the new form, when I assigned the value, I needed to say:

txtAccount.text = frmTrans.intAccount

So, it's working. Yah! lol
 
Since frmTrans is calling the frmPatientEntry I would let frmTrans stay in control of the operation. In frmPatientEntry I would add a Public method InitializeForm with the string parameter. From frmTrans you can then create the instance of frmPatientEntry, use the intialization method, show the form. The method could be like this:
VB.NET:
Public Sub InitializeForm(textboxtext As String)
  txtAccount.text = textboxtext
End Sub
And it frmTrans it could be like this:
VB.NET:
Dim f As New frmPatientEntry
f.InitializeForm(intAccount.ToString)
f.Show()
 
Using Shared members is appropriate in some situations but it is often used as a lazy way to achieve an end that can be easily and better achieved using other means. I suggest that you follow the first link in my signature and read all four pages. That's a good place to start.
 
fyi You can access any public method or property from any object

in form1

public myval as string = ""

in form2
dim frm as new form2
form2.myval = "What Ever"
form2.show

or

form1

public sub new(byval something as sometype)
mybase.new
mytypeproperty = something
end sub

in form2

dim frm as new form2(mytypeclass)
frm.show
 
Back
Top