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!

checkBox

Status
Not open for further replies.

macl

Programmer
Sep 15, 2004
65
DE
Hello, i have a question
have a form from which the user can open up a new from on this second form he shall be able to check or uncheck a checkBox, then he closes the second form again. (the second form is closed by cmdok_click() with Me.unload at the end of the code)
Now i would need to know in the first form if the checkbox in the second form is checked or not.
I guess i could do that by using a global variable but is there a different way than that too?
thanks alot :)
 
Yes there is a way which is preferred over storing values in global variables.

You should not unload your form in the OK button click. Instead of unloading you should hide your form using Hide method. This returns the control back to the calling code in Form1 without unloading Form2. After retrieving the value of checkbox or any other control, you should manually unload Form2.

See the following example.

Start a new Project, place a command button and the following code in Form1.
___
[tt]
Private Sub Form_Load()
Command1.Caption = "Show Form2"
End Sub
Private Sub Command1_Click()
Dim CheckVal As Integer
Form2.Show vbModal
CheckVal = Form2.Check1.Value
Unload Form2
MsgBox "Form2.Check1.Value = " & CheckVal
End Sub[/tt]
___

Now add a new form to the project and place a command button and a check box on it. Insert the following code in Form2.
___
[tt]
Private Sub Form_Load()
Command1.Caption = "OK"
End Sub
Private Sub Command1_Click()
Hide
End Sub[/tt]
___

Now run the program and test it. Note that you must not close Form2 using the Close button on the titlebar or control menu, because it unloads the form instead of hiding it. You should only use the OK button to do this.

It is better to remove access to the control box by setting the ControlBox property of Form2 to False.
 
Another option is to use a public modular variable in Form1 and assign it the value of the form2 checkbox before form2 is unloaded.

zemp
 
thanks for you suggestiones, it worked :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top