+ Reply to Thread
Results 1 to 5 of 5

passing variable from userform

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    passing variable from userform

    I am using the following code to open a userform. Once the userform is open, the user has the option to choose a continue button or a cancel button. If the user is choosing continue, the code is not working (it always skips to the "else" portion of my "if" statement. I can't figure out why it is doing this. Thanks. I have attached a sample of workbook, if needed.
    Sub UpdateMasterFile()
    
    Dim Continue As String
    
        Load DisclaimerForm
        DisclaimerForm.Show
        
        If Continue = "Yes" Then
            'do some updates
            MsgBox "Update Complete!  ", vbInformation
        Else
            MsgBox "Update Canceled!  ", vbInformation
        End If
    
    End Sub
    Userform code:
    Private Sub CheckBox1_Click()
    
        If CheckBox1.Value = True Then
            ContinueButton.Enabled = True
        Else
            ContinueButton.Enabled = False
        End If
        
    End Sub
    Private Sub ContinueButton_Click()
        Unload DisclaimerForm
        Continue = "Yes"
    End Sub
    Private Sub CancelButton_Click()
        Unload DisclaimerForm
        Continue = "No"
    End Sub
    Attached Files Attached Files
    Last edited by maacmaac; 01-31-2010 at 08:22 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: passing variable from userform

    Hi maacmaac
    all you needed to do was call the sub

    try....


    Private Sub ContinueButton_Click()
    Unload.DisclaimerForm
    Continue = "Yes"
    UpdateMasterFile
    End Sub
    
    Private Sub CancelButton_Click()
        Unload DisclaimerForm
        Continue = "No"
       UpdateMasterFile
    End Sub
    
    Sub UpdateMasterFile()
    
       If Continue = "Yes" Then
            'do some updates
            MsgBox "Update Complete!  ", vbInformation
        Else
            MsgBox "Update Canceled!  ", vbInformation
        End If
    
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: passing variable from userform

    You could change the userform's code to this. (Note the added function.)
    Private Sub CheckBox1_Click()
        If CheckBox1.Value = True Then
            ContinueButton.Enabled = True
        Else
            ContinueButton.Enabled = False
        End If
    End Sub
    
    Private Sub ContinueButton_Click()
        Me.Hide
    End Sub
    
    Private Sub CancelButton_Click()
        Unload Me
    End Sub
    
    Public Function ContinueUpdate() As Boolean
        Me.Show
        ContinueUpdate = DisclaimerForm.CheckBox1.Value
        Unload DisclaimerForm
    End Function
    and change the calling routine to
    Sub UpdateMasterFile()
        If DisclaimerForm.ContinueUpdate Then
            'do some updates
            MsgBox "Update Complete!  ", vbInformation
        Else
            MsgBox "Update Canceled!  ", vbInformation
        End If
    End Sub
    The technique is like returning the value of a function in a custom Class.
    Note: In the Function ContinueUpdate, all references after the Me.Show line should be to DisclaimerForm rather than Me, in case the user Cancels or corner X's.
    Attached Files Attached Files
    Last edited by mikerickson; 01-31-2010 at 04:11 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: passing variable from userform

    Pike's solution will solve your problem.

    The reason it does not work is due to the scope of your variables.
    The variable Continue declared in the UpdateMasterFile routine is not the same variable used in the button click events.

    http://www.ozgrid.com/VBA/variable-scope-lifetime.htm
    Cheers
    Andy
    www.andypope.info

  5. #5
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Re: passing variable from userform

    Code is working fine now. Don't quite understand how the "me" commands work (me.show, me.hide, unload me, etc.). Something I need to study. If you have any recommended links and/or books that explains in depth, I would appreciate it. Thanks for all the comments.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1