Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a value from one form to another

Status
Not open for further replies.

jpinto

Technical User
Dec 12, 2003
75
PT
Hello,

Probably this is very simple but I'm stuck here with this problem:

On a form I've a text box with a value that I want to assign to a public variable so that I can use it in another form. I've made this:

1st form:

refer = ReferenciaInput

2nd form:

Public refer As String

Private Sub Form_Load()
MsgBox (refer)

In the 2nd form, the variable refer is empty!

Can annyone help me please?

Thanks,

João Pinto
 
Most basic way is to add a Code Module to the Project (bas)., then in there declare a public variable.

public refer as text

Then it is reusable from any form. So the code as you had above will work.

Casper

There is room for all of gods creatures, "Right Beside the Mashed Potatoes".
 
As I suspected, it was simple to solve my problem. I'm new to VB.
I've already tried and is working.

Thanks,

João Pinto
 
You can also define public functions/subs on forms and call them passing the information you require as parameters.


"I'm living so far beyond my income that we may almost be said to be living apart
 
True enough you can use a global public var.
I'm kinda just re-learning vb as well.
What I did is:

Form1
Private mlngContactID as variant
in form1 (declarations section)

Form2

Public Property Get ContactID() As Long
On Error GoTo Get_ContactID_Error

ContactID = mlngContactID

Get_ContactID_Done:
Exit Property
Get_ContactID_Error:
Call Process_Error(MODULE_NAME, "Get_ContactID")
Resume Get_ContactID_Done

End Property

Public Property Let ContactID(ByVal vlngData As Long)
On Error GoTo Let_ContactID_Error

mlngContactID = vlngData

Let_ContactID_Done:
Exit Property
Let_ContactID_Error:
Call Process_Error(MODULE_NAME, "Let_ContactID")
Resume Let_ContactID_Done

End Property

From form1 I

Dim form2 as new form2
from2.ContactID = mlngContactID

Hope this helps.
 
What jasherk has done is probably the best if you need to validate the input for the actual field prior to setting it, but generally you would use properties when defining classes or objects.
All are valid ways, but most times I would see global variables implemented via a Module. this module would solely hold global variables and constants. Then other modules would exist containing public sub routines etc etc.
2c


"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top