|
-
Sep 22nd, 2005, 12:56 PM
#1
Thread Starter
Lively Member
[RESOLVED] Another VB Mystery??
Hi again. I have one form (form1) opening another form (form2) as a dialog box. The user may enter some data into form2, then will press either cmdOK or cmdCancel to finish.
I want form1 to be able to know which button the user pressed in form2. I tried it like this;
VB Code:
{form1}
load form2
form2.show
{form2}
Private Sub cmdCancel_Click()
Me.Visible = False
End Sub
{form1 again}
if form2.cmdCancel.value then
(disregard the operation because they pressed the cancel cmd button)
endif
In the form2 cmd_click sub, the cmdCancel property is TRUE after they press Cancel. But as I step thru the program, I notice that when it returns to form1, the cmdCancel value is set to FALSE.
Any ideas how form1 can read the values of the form2 command buttons?
Thanks again - Tom
Last edited by Tom951; Sep 22nd, 2005 at 01:00 PM.
-
Sep 22nd, 2005, 12:58 PM
#2
Fanatic Member
Re: Another VB Mystery??
You could store the values you want to save in a command module and refer to them from anywhere in your program.
-
Sep 22nd, 2005, 01:02 PM
#3
Thread Starter
Lively Member
Re: Another VB Mystery??
You mean store them in a global variable?
-
Sep 22nd, 2005, 01:08 PM
#4
Hyperactive Member
Re: Another VB Mystery??
VB Code:
Form2.Show vbModal, Me
If Form2.Cancelled Then
Debug.Print "Cancelled"
Else
Debug.Print "OK"
End If
Set Form2 = Nothing
Where the code for the dialog (Form2) is:
VB Code:
Public Cancelled As Boolean
Private Sub CancelButton_Click()
Cancelled = True
Unload Me
End Sub
Private Sub Form_Load()
'Assume Cancelled.
Cancelled = True
End Sub
Private Sub OKButton_Click()
Cancelled = False
Unload Me
End Sub
-
Sep 22nd, 2005, 01:11 PM
#5
Thread Starter
Lively Member
Re: Another VB Mystery??
Thanks, I understand. (Baaarrrrrrffffff) 
I guess this means that the form2 variables are not available after the "End Sub" for that module is executed?
-
Sep 22nd, 2005, 01:14 PM
#6
Re: Another VB Mystery??
Here's how you can do it
VB Code:
{form1}
form2.Show vbModal
If form2.Cancelled Then
(disregard the operation because they pressed the cancel cmd button)
End If
{form2}
Public Cancelled As Boolean
Private Sub cmdCancel_Click()
Cancelled = True
Me.Hide
End Sub
Private Sub cmdOK_Click()
Cancelled = False
'
'Other stuff
'
Me.Hide
End Sub
Private Sub Form_Load()
Cancelled = False
End Sub
Pradeep
-
Sep 22nd, 2005, 01:16 PM
#7
Re: Another VB Mystery??
 Originally Posted by Tom951
Thanks, I understand. (Baaarrrrrrffffff)
I guess this means that the form2 variables are not available after the "End Sub" for that module is executed?
All variables from both forms would be available to each form if you declare them as Public.
-
Sep 22nd, 2005, 01:19 PM
#8
Thread Starter
Lively Member
Re: Another VB Mystery??
Along these lines, it would be awfully convenient if I could call a form with parameters -- something like;
VB Code:
{form1}
istatus = Form2 (data1, data2)
If istatus = Good
( process data1 & data2)
Else
( disregard )
Endif
Maybe I'm a throwback. - Tom
-
Sep 22nd, 2005, 01:21 PM
#9
Hyperactive Member
Re: Another VB Mystery??
 Originally Posted by Tom951
I guess this means that the form2 variables are not available after the "End Sub" for that module is executed?
In my example, the Form2 variables are not available after the:
-
Sep 22nd, 2005, 01:22 PM
#10
Re: Another VB Mystery??
Ohhh... bpd wins
Pradeep
-
Sep 22nd, 2005, 01:47 PM
#11
Thread Starter
Lively Member
Re: Another VB Mystery??
There is definitely an inconsistency here.
In form1, I can read the whatever the user typed into data1 and data2 just fine after I come back from form2.
However, the value property of the form2.cmdCancel button gets set to False when the form2 procedure ends, even though the user pressed it to exit form2. In the form2 cmdCancel Clicked subroutine, it is set to True.
Comments?
-
Sep 22nd, 2005, 01:57 PM
#12
Re: Another VB Mystery??
The value property of command buttons remain true only when it is clicked and you are within it's procedure. As soon you move out of Command1_Click, it will become false (it has no relation to form loading/unloading).
Pradeep
-
Sep 22nd, 2005, 11:55 PM
#13
Re: [RESOLVED] Another VB Mystery??
I didn't even know command buttons had a Value property.
Anyway, I suppose you are making a custom Messagebox or something, so here's how you'd do that, with parameters:
VB Code:
' In a module somewhere
Enum CustomMsgBoxResult
mbOK
mbCancel
End Enum
Function CustomMsgBox(ByRef pszData As String, ByRef pszCaption As String) As CustomMsgBoxResult
With <message box form>
.lblData = pszData
.Caption = pszCaption
.Show vbModal
CustomMsgBox = .Result
End With
End Function
VB Code:
' In the form
Private mResult As CustomMsgBoxResult
Property Get Result() As CustomMsgBoxResult
Result = mResult
End Property
Private Sub cmdOK_Click()
Result = mbOK
Me.Hide
End Sub
Private Sub cmdCancel_Click()
Result = mbCancel
Me.Hide
End Sub
HTH
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|