Results 1 to 13 of 13

Thread: [RESOLVED] Another VB Mystery??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    Resolved [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:
    1. {form1}
    2.     load form2
    3.     form2.show
    4.  
    5. {form2}
    6. Private Sub cmdCancel_Click()
    7.     Me.Visible = False
    8. End Sub
    9.  
    10. {form1 again}
    11.     if form2.cmdCancel.value then
    12.          (disregard the operation because they pressed the cancel cmd button)
    13.     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.

  2. #2
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    Re: Another VB Mystery??

    You mean store them in a global variable?

  4. #4
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Another VB Mystery??

    VB Code:
    1. Form2.Show vbModal, Me
    2. If Form2.Cancelled Then
    3.   Debug.Print "Cancelled"
    4. Else
    5.   Debug.Print "OK"
    6. End If
    7. Set Form2 = Nothing
    Where the code for the dialog (Form2) is:
    VB Code:
    1. Public Cancelled As Boolean
    2.  
    3. Private Sub CancelButton_Click()
    4.   Cancelled = True
    5.   Unload Me
    6. End Sub
    7.  
    8. Private Sub Form_Load()
    9.   'Assume Cancelled.
    10.   Cancelled = True
    11. End Sub
    12.  
    13. Private Sub OKButton_Click()
    14.   Cancelled = False
    15.   Unload Me
    16. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    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?

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Another VB Mystery??

    Here's how you can do it
    VB Code:
    1. {form1}
    2.     form2.Show vbModal
    3.     If form2.Cancelled Then
    4.          (disregard the operation because they pressed the cancel cmd button)
    5.     End If
    6.  
    7.  
    8. {form2}
    9. Public Cancelled As Boolean
    10. Private Sub cmdCancel_Click()
    11.     Cancelled = True
    12.     Me.Hide
    13. End Sub
    14. Private Sub cmdOK_Click()
    15.     Cancelled = False
    16.     '
    17.     'Other stuff
    18.     '
    19.     Me.Hide
    20. End Sub
    21. Private Sub Form_Load()
    22.     Cancelled = False
    23. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Another VB Mystery??

    Quote 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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    Re: Another VB Mystery??

    Along these lines, it would be awfully convenient if I could call a form with parameters -- something like;

    VB Code:
    1. {form1}
    2.  
    3.     istatus = Form2 (data1, data2)
    4.  
    5.     If istatus = Good
    6.         ( process data1 & data2)
    7.     Else
    8.         ( disregard )
    9.     Endif

    Maybe I'm a throwback. - Tom

  9. #9
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    468

    Re: Another VB Mystery??

    Quote 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:
    VB Code:
    1. Set Form2 = Nothing

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Another VB Mystery??

    Ohhh... bpd wins

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    107

    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?

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. ' In a module somewhere
    2. Enum CustomMsgBoxResult
    3.     mbOK
    4.     mbCancel
    5. End Enum
    6.  
    7. Function CustomMsgBox(ByRef pszData As String, ByRef pszCaption As String) As CustomMsgBoxResult
    8.     With <message box form>
    9.         .lblData = pszData
    10.         .Caption = pszCaption
    11.  
    12.         .Show vbModal
    13.  
    14.         CustomMsgBox = .Result
    15.     End With
    16. End Function

    VB Code:
    1. ' In the form
    2.  
    3. Private mResult As CustomMsgBoxResult
    4.  
    5. Property Get Result() As CustomMsgBoxResult
    6.     Result = mResult
    7. End Property
    8.  
    9. Private Sub cmdOK_Click()
    10.     Result = mbOK
    11.     Me.Hide
    12. End Sub
    13.  
    14. Private Sub cmdCancel_Click()
    15.     Result = mbCancel
    16.     Me.Hide
    17. 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
  •  



Click Here to Expand Forum to Full Width