954,558 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

passing data between forms

How do you pass the data in a field on one form to a field in another form?

stan yost
Light Poster
29 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

Specify formName.fieldName.data

WaltP
Posting Sage w/ dash of thyme
Moderator
10,507 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

WaltP is correct. You can also call functions, events and methods this way too. For example, if you have a button on form2, and you want to "click" it from a button on form1, you can do something like form2.command1 = true .

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

stan yost there is a simple way

lets say you want to pass the text feild of text1

1- declare a public variable in the general part of the form like this

public varaiableName as String

2- in the form_load() write

variableName=text1.text

3- you can access this variable from any other form using the form name

lets say you have form2 and you want to pass text1.text which is in form1 to form2 which has a text feild (text2)

you can do that like this:-

text2.text=form1.variableName

- Regarding calling methods You have first to decalre it as public intead of private sub ---> public sub

and u can do the same thing using the form name to call the function

form1.functionName()


I hope this helps

vbmenu_register("postmenu_246662", true);

lover99509
Newbie Poster
21 posts since Jul 2006
Reputation Points: 15
Solved Threads: 1
 
stan yost there is a simple way lets say you want to pass the text feild of text1 1- declare a public variable in the general part of the form like this public varaiableName as String 2- in the form_load() write variableName=text1.text 3- you can access this variable from any other form using the form name lets say you have form2 and you want to pass text1.text which is in form1 to form2 which has a text feild (text2) you can do that like this:- text2.text=form1.variableName - Regarding calling methods You have first to decalre it as public intead of private sub ---> public sub and u can do the same thing using the form name to call the function form1.functionName() I hope this helps vbmenu_register("postmenu_246662", true);

lover99509, is this easier than using form1.text1.text as already recommended?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,507 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

lover's method is accurate, and will work.... however, it's not good programming style. As Waltp Mentioned, it's easier first off, to reference the object hierarchy, and beyond that, why would you want to use memory space (a variable) to contain information that's ALREADY stored somewhere? That will slow down the program (more processing, and memory usage) and make code a bit more difficult to read....

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Thanks, that got me going again.

stan yost
Light Poster
29 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

I'm working on something like this and I have the following situation:

I guiding users through a wizard process to select or create various ID's. These ID's are set at the end of the proess when closing the form.
So where I need the ID I start a form in Dialog mode and on closing the new form I set a Public variable in a separate module to the value of this ID.
Within the wizard process I need to do this 2 or three times.
When the dialog form closes the originating form picks up the ID and works with that.

As it now appears the value is not passed between the forms. I cannot refer to the form's variables because at the time I need to pick up the alue the form is already closed.

Using watches I could determine that withn the context of each form the variable doesnot appear to be set.

I refer in the entire project to this variable as GlobalFunctions.SelectedID and in the module GlobalFunction it has been declared as:

Public SelectedID As Long


Unfortunately, the selection process is calledfrom different forms so I cannot define a button that go's back to the originating form that will close the selection form.

Any thoughts on this?

Many thanks in advnce
Hans

HansyWolf
Newbie Poster
2 posts since Sep 2006
Reputation Points: 10
Solved Threads: 1
 

I might not understand you completely, but if you are asking what I think you are asking, setting a global variable should work among all forms. I just want to make sure that you are using VB6, and not VB.Net or VB express..... Because I don't believe VB6 has a GlobalFunctions Object.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Thanks fort an answer. I think you understand what I'm doing. I'm programming within MS Acces so VBA. The module GlobalFunctions is my own fabrication.

The problem is it works two or three times in arow and then one time it does not pass the right value.


Greets
ans

HansyWolf
Newbie Poster
2 posts since Sep 2006
Reputation Points: 10
Solved Threads: 1
 

Hi
I am working on a payroll program which have 3 forms.
First form has three text box (First name, Surname, Total Hour) and one command button Calculate.
After the user put the info he/she will click calculate button and this will show the second form which display the (firstname, surname, Gross salary, netwage).
I do have another form called frmsummary this form should hold detail of up to 10 person, currently using the code bellow in form two when user click copy info command button on the second form I could only copy 1 person info to form 3. I don’t know what to do any help to get 10 people info coped?

Private Sub cmdcd_Click()
frmsummary.lblfname0 = frmWage.lblfirstname.Caption
frmsummary.lblsname0 = frmWage.lbllastname.Caption
frmsummary.lblgsalary = frmWage.lblGrossSalary.Caption
frmsummary.lblnwage = frmWage.lblNetWage.Caption
frmsummary.Show
End Sub

Thanks for your help in advance
HB25

HB25
Junior Poster in Training
74 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 
it's easier first off, to reference the object hierarchy ,and beyond that, why would you want to use memory space (a variable) to contain information that's ALREADY stored somewhere? That will slow down the program (more processing, and memory usage) and make code a bit more difficult to read....

That's simply not the case. Declaring objects as variables saves Visual basic from having to go through the same code again. Once VB has it declared as a variable, it saves processing time.

Hank

hkdani
Posting Pro in Training
435 posts since Nov 2007
Reputation Points: 49
Solved Threads: 47
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You