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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arguments to another form 1

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
Maybe someone can help me with this. It's probably so simple it is taken for granted in all the books, but I still don't understand how to do this effectively.

Lets' say I have an app that allows a user to select a record from a database, then it calls another form which is used to display data about the record selected.

I want to pass the record key from the first for to the second form without having to store it in a public variable, where the first form puts it and the second form reads it. I want to pass it directly to the second form when I issue it the show command.

Is it possible to pass arguments directly to a form-level variable? Is the answer calling a procedure in the second form from the first form, and passing the arguments that way? If I want to return a value back to the originating form, do I need to make this procedure a function?

What would be the best way of doing this?
 
Craig,
I'm not sure exactly what you are asking, but . . .

if I have a form (form1) with record set info sitting in say, a text box, then I can access the text box info from form2 simply by saying form1.textbox1.text

so, if you don't want to declare your variable public, then you could always just place it in a text box and access the textbox from another form.

just a thought.
 
re: faq222-400 and/or thread222-37333 _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thanks for responding, Bill

So if I understand what you are saying, I should not try to 'push' the arguments to the new window on opening, but 'pull' them from the first window once the second window is established (probably in the form_load procedure).

Do I have that right?

Craig
 
CraigHartz you have it correct, in your second form, just refer to the object in the previous form that has the value you want.

You were thinking you would need to pass the values to the form when you invoked the show command, much like this:

frmAnotherForm.Show(strVariable1, strVariable2)

However, as you just said, instead of "pushing" them to the form, you just make the new form "pull" them.

Say on frmMain you had a text box that you wanted that same value to show up on a label in a form that pops up later:

Code:
'frmMain
  txtStatus.Text = "Online"
  frmSecondary.Show

'frmSecondary
Private Sub Form_Load()
  lblStatus.Caption = frmMain.txtStatus.Text
End Sub

They key that you need to pay attention to is this:

When referring to a control on another form, you must specifically point VB to the form where the control exists. If txtStatus is on frmMain, you have to specifically say frmMain.txtStatus and if that text box is on frmOptions, same thing, frmOptions.txtStatus

Also, as far as where you put the code, you need to put it in the Form_Load and AFTER ANY CODE that would cause the value to change or need to be changed. The Form_Activate would work as mentioned above, if you were to change the value of the control on the original form where you are pulling the value.
 
CraigHartz,

I would do it some what differently. I use a property get and property let method.

Lets say you have a list box (ListBox1) and a command button (Command1) on Form1. The list index contains your key field and the item data your text.

You want to pass the listindex to Form2 and display in a text box on Form_Load.

On Form1 code the Command1_Click event to look like the following:

Private Sub Command1_Click()
With Form2
.EmployeeID = CLng(Val(Trim$(ListBox.ItemData _
(ListBox.ListIndex))))
.Show
End With
End Sub

On Form2 code the General Declaration, Form_Initialize, and the two properties to look like the following:

[General Decs]
mlngEmployeeID

Private Sub Form_Initialize()
'so that we know a value has been passed we first set
'mlngEmployeeID to -1
mlngEmployeeID = -1
End Sub

'Property get can be changed to recieve strings
Property Get EmployeeID() As Long
EmployeeID = mlngEmployeeID
End Property

'Property let can also be changed to receive strings
Property Let EmployeeID(ByVal lngvalue As Long)
mlngEmployeeID = lngvalue
End Property

Private Sub Form_Load()
TextBox1.Text = mlngEmployeeID
End Sub

Anyway, I guess this is just one other method for accomplishing this without using global variables. I think it is extremely tidy.

Rossco
:-}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top