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

Use data from one form in another form 4

Status
Not open for further replies.

jsgs

Instructor
Aug 22, 2008
47
Hi to all!

I have a form (let's say form1) with 10 command buttons on it. When I click on any one button, another form (form2 - similar to an inputbox) appears asking to input a name.

Form1 is the main form and all my code are in form1.
Form2 is just there to get the name and validate it.

Problem: I want to use the name entered in form2 in form1. I've tried writing this code in form1:
strName = forms.form2.txtName.Text
but it doesn't work.

Anyone can please help as to how this can be done? Thank you in advance.

 
I assume you are loading form2 as vbModal. When you are done with form2, you should Hide it, not Unload it. Then you can get the form2.txtName.Text value. After that then do the following

unload form2
set form2 = nothing
 
SBerthold, I've tried this and it doesn't work as well.

waytech2003, I just use the command form2.show to load the form. And as you said I don't unload it but still strName does not get the data in the textbox.
 
>SBerthold, I've tried this and it doesn't work as well.

Are you closing the form2 prior to using this?
If this is just an Input form, then you can open Form2 as a vbModal form (see the first code box below):
Code:
'Command button click:
Private Sub cmdOpenForm2_Click()
    Dim frmInput As Form2
    Dim Results As String
    Set frmInput = New Form2
    
    frmInput.Show vbModal
    
    Results = frmInput.txtName.Text
    
    Unload frmInput
    Set frmInput = Nothing
End Sub

Then either add a "Close" button to Form2, allowing the user to close it only by this method, (by setting the ControlBox property of Form2 to 'False'), or, as shown in the below code box, prevent an Unload of Form2 when the user closes the form by clicking the 'X':
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode <> vbFormCode Then
        Cancel = vbTrue
        Me.Hide
    End If
End Sub
 
Sorry, but some how I meant to change the second code, but didn't.
Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = [b]vbFormControlMenu[/b] Then
        Cancel = vbTrue
        Me.Hide
    End If
End Sub
If you additionally use a command button on Form2 to close the form, then in the button's Click event also use Me.Hide.

 

Okay, assuming that jsgs only wants to retrieve a singular string value (meaning one variable containing a string)...

SBerthold, jsgs here is a test project for you...

Start vb new standard exe and you have Form1. Add Command1 and the following code...
Code:
Option Explicit

Private Sub Command1_Click()
Dim S As String

S = Form2.WhoCalled(Me)

MsgBox S

End Sub

Now add a new form (Form2) and place upon it Text1 and Command1 and the following code...
Code:
Option Explicit

Public Function WhoCalled(F As Form) As String
Me.Show vbModal, F
WhoCalled = Text1.Text
End Function

Private Sub Command1_Click()
Unload Me
End Sub

Explanation of how it works...
form1.command1 calls the public function in form2.whocalled and passes it a reference to itself as an arguement (F as Form). This causes form2 to load so if you need to do any type of setup for form2 you can.

Now inside of whocalled you can see that form2 takes care of showing itself vbmodal to the owner form Form1. User then enters their information in text box and then clicks on command1 (or hits enter if you set the button up as default).

Then in the click event of Form2.Command1 we simply call unload me. From there, the next line to execute is whocalled=text1.text thus passing the value back to our declared S as string variable so we can use in in the test msgbox.

Just a little OO design for you.

Good Luck

 
vb5prgrmr said:
SBerthold, jsgs here is a test project for you...
vb5prgrmr, I know how to do this. Most forms I write to open vbModal and show themselves.

Also, what I wrote about using a close button was also in light of what waytech2003 posted, and I added additional code for the QueryUnload, (though should have added a msgbox there, if the changes should be accepted, and not accepted automatically).

vb5prgrmr said:
Explanation of how it works...
vb5prgrmr said:
Then in the click event of Form2.Command1 we simply call unload me. From there, the next line to execute is whocalled=text1.text thus passing the value back to our declared S as string variable so we can use in in the test msgbox.

Your posted code will not work, as you obviously assume it does.
You need to code as waytech2003 mentioned.

 

SBerthold,
You are so correct! I was going from memory and the code will return the default value of the text box but the code in Form2 below will work.
Code:
Option Explicit

Dim S As String

Public Function WhoCalled(F As Form) As String
Me.Show vbModal, F
WhoCalled = S
End Function

Private Sub Command1_Click()
S = Text1.Text
Unload Me
End Sub

Good Luck

 
Or just:
Code:
Public Function WhoCalled(F As Form) As String
    Me.Show vbModal, F
    WhoCalled = Text1.Text
    Unload me
End Function

Private Sub Command1_Click()
    me.Hide
End Sub

 

Meaning that your version or mine both work. i.e. there is more than one way to skin a cat...
 
There is another way. Here's how I handle things in my application.

Suppose you have a button on the main form. When this button is pressed, you want to open another form, get some input from the user, and return that data to the main form.

Here's how I do it.

In form2, I create a public subroutine that is called by the main form. This subroutine has byref parameters to represent the data I want to capture. The main form calls this public subroutine. The public subroutine sets default values on the form and then calls Show(vbModal), and then finally passes the data back to the calling form.

Try it:

Open a new vb project (standard exe).
By default 1 form will already be added. Add a second form.
On form 1, add a text box (text1) and a button Command1
On form 2, add a text box (Text1) and 2 buttons (Command1 and Command2).

The caption for Command1 should be 'OK' and the caption for command2 should be 'Cancel'.

With this structure, you can keep the control box on the form because you set the cancel flag to true before showing the form. If the user clicks the X in the upper right corner of the form, the form is unloaded and the cancel flag is true.

The code for form1:

Code:
Option Explicit

Private Sub Command1_Click()
    
    Dim Data As String
    Dim Cancel As Boolean
    
    Data = Text1.Text
    Call Form2.GetUserInput(Data, Cancel)
    If Not Cancel Then
        Text1.Text = Data
    End If
    
End Sub

The code for Form2:
Code:
Option Explicit

Private bCancel As Boolean
Private sData As String

Public Sub GetUserInput(ByRef Data As String, ByRef Cancel As Boolean)

    Text1.Text = Data
    
    bCancel = True
    
    Call Show(vbModal)
    
    Cancel = bCancel
    Data = sData

End Sub

Private Sub Command1_Click()
    
[green]    ' you can put validation code here
    ' and pop up an error message if the 
    ' validation fails.
    ' 
    ' You will also need to exit the sub
    ' so that Unload Me is not called.[/green]
    sData = Text1.Text
    bCancel = False
    
    Unload Me
    
End Sub

Private Sub Command2_Click()
    
    Unload Me
    
End Sub


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Or

Form2 code
Code:
[blue]Option Explicit

Public mInfo As String

Private Sub Command1_Click()
    Unload Me
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    mInfo = Text1.Text
End Sub[/blue]

Form1 code:
Code:
[blue]Option Explicit

Private Sub Command1_Click()
    Form2.Show vbModal
    Text1.Text = Form2.mInfo
End Sub[/blue]

Unloading a form really does no such thing; it just gets rid of the graphical bits. Since a textbox is a graphical elemnt it gets unload and you lose the contents. Public variables belonging to the form, on the other hand, remain available (and, unlike trying to access a graphical element of an unliaded form, does not casuse the form to reload) ... so, as long as you transfer the contents of your graphical controls to public variables they are still available after the form has unloaded
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top