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 strongm 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 to a textbox

Status
Not open for further replies.

tekomni

Programmer
Nov 25, 2002
40
US
I have a MDI application in which I do somthing like:

dim frm as new frmName
frm.show

from the form "frmName" I load another form which needs to pass a value back to the "frmName".

The second form has a public variable txtOwner as textbox

I set txtOwner = "frmName.txtInput" before loading the second form

Problem: the "frmName.txtInput" isnt change due to:
dim frm as new frmName

the textbox is never updated. Im playing around with the ByRef/ByVal flags but i can't get it to work, any ideas? Thanks
 
tekomni

I am not sure fully understand you problem, but I have a suggestion:

Code:
dim frm as new frmName
frm.show

should be replace with

Code:
dim frm as frmName
set frm = new frmName
frm.show

The essential difference between the 2 is that frm is created explicitly by the Set statement. If you use the din frm as new, the object is not created until it is first referenced. Using the Set will make the object (form) lifetime much easier to control. That is, currently, if you destroy the form and then reference a property (or control)you will create a new instance of the form.

Using the Set frm= new construct will also speed up execution (marginally) as the the runtime will not need to check whether frm exists or no.

I would suggest that you try that, it may not solve the problem but it is (IMHO) better coding.

Hope that helps


Matt

 
Try this example that i did.

You need 2 forms name it as Form1 and Form2 and
a class module name it as IOwner

In Form1 you need :

1 Textbox : Text1
1 Command button : Command1

In Form2 you need :

1 Textbox : Text1
1 Command button : Command1

In Code of Form1 paste this code

''''
Private mOwner As IOwner

Private Sub Command1_Click()
mOwner.GetName (Text1)
Unload Me
End Sub

Public Property Set Parent(frm As IOwner)
Set mOwner = frm
End Property
''''

In Form2 paste this code

'''''
Implements IOwner

Private Sub Command1_Click()
Dim frm As Form1

Set frm = New Form1
Set frm.Parent = Me
frm.Show

End Sub

Private Sub IOwner_GetName(Name As String)
Text1 = Name
End Sub
'''''

In Class Module IOwner paste this code

'''''
Public Sub GetName(Name As String)
End Sub
'''''
 
What this program do is Run the Form2 then when Command1 of Form2 is clicked it shows Form1 and set the Form2 as the Parent of Form1 IOwner object. And when the Command1 of Form1 is clicked it sets value of text1 to the parameter of the procedure GetName of IOwner object.

Since Form2 implements IOwner object you can get the value name thru the event of the IOwner object.

Hope this solve your problem..
 
tekomni,
Usually what I do to achieve your goal is something like this:

Code in Form1
Event UnloadForm(strText As String)

Private Sub Form1_Unload()
'during the unload, raise the event and pass
'the text you want to return,
'in this example, i'm returning text
'from Form1.txtSomething textbox
RaiseEvent UnloadForm(txtSomething.Text)
End Sub

Code in Form2
Private WithEvents frm1 As Form1

Private Sub Command1_Click()
'loads form2
Set frm1 = New Form1
frm1.Show
End Sub

Private Sub frm1_UnloadForm(strText As String)
'this event will be fired during Form1 unload
txtInput.Text = strText
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top