+ Reply to Thread
Results 1 to 3 of 3

Thread: VBA Sub Procedures

  1. #1
    Registered User
    Join Date
    03-16-2008
    Posts
    3

    VBA Sub Procedures

    I have this homework assignment that confuses the hell out of me. And i need help. I'm so confused please help me

    When you open the Excel file, you will only see two buttons, Start and Exit. Now, click on the Design Mode button, you will see the third button, Select. This is because the buttons have an additional property Visible, in addition to the Enabled property that you used in MP4. In this part, you will create a subprocedure that sets the Enabled and Visible properties of the three buttons, Start, Select, and Exit. You can see that in the file given to you, the Start button is enabled and visible, Select button is disabled and invisible, and Exit button is disabled and visible.
    The voting process is started when the user clicks on the Start button. When voting starts, Start button should get disabled, Select button should get enabled and visible, and Exit button should get enabled. When the voting process ends, the user clicks on the Exit button and then the exact opposite should happen: Start button should get enabled, Select button should get disabled and invisible, and Exit button should get disabled (Like the way it is initially in the file given to you).
    Here is a summary of the desired functionality:

    * Start voting
    o Start button disabled
    o Select button visible
    o Select button enabled
    o Exit button enabled

    blnStart = True

    * Stop voting
    o Start button enabled
    o Select button invisible
    o Select button disabled
    o Exit button disable

    blnStart = False

    Notice how Start voting and Stop voting change the same set of properties but in complete opposite value. Since, we want to either do a set of actions or just the opposite set of actions, we do not need to write two subprocedures. We can write one sub-procedure which takes a Boolean input and does one set of actions when the input is True and does the opposite set of actions when the input is False. In other words, the buttons can be enabled/disabled or made visible/invisible depending on the value of this boolean variable.
    Do the following in your VBA Editor:

    1. Declare a subprocedure called ToggleButtons that takes in one Boolean input, blnStart.
    2. Write 4 lines of code that uses the subprocedure's input to assign the 4 properties: cmdStart.Enabled, cmdSelect.Visible, cmdSelect.Enabled, and cmdExit.Enabled. The input will be True if the user is starting the voting process and False if the user is ending the voting process. Remember that the Enabled and Visible properties are also Boolean values, so you can assign them blnStart or Not blnStart.
    3. Update the subprocedures cmdStart_Click and cmdExit_Click to call the subprocedure ToggleButtons that you just created and pass in an appropriate value for the input.

    Test: If things are working correctly, you should be able to click on the Start button to have the buttons change to the desired status. The Exit button should then be enabled for you to click, and you will be back to the original state. If your buttons disappear while testing the code that you wrote, you can go into Design mode and change the enabled and visible properties through the Properties window. However, finally you should have them as desired by the MP.

  2. #2
    Registered User
    Join Date
    03-16-2008
    Posts
    3
    Here is what i got

    Private Sub cmdStart_Click()
    
    Dim blnStart As Boolean
    
    blnStart = cmdStart.Enabled = True
    blnStart = cmdExit.Enabled = False
    blnStart = cmdSelect.Enabled = False
    blnStart = cmdSelect.Visible = False
    
    End Sub

  3. #3
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639
    You are being asked to create one sub procedure that can be called by the buttons. This is what your project is asking for

    Option Explicit
    Dim bStart     As Boolean
    Private Sub CommandButton1_Click()
        bStart = True
        doButtons
    End Sub
    
    Private Sub CommandButton3_Click()
        bStart = False
        doButtons
    End Sub
    
    Sub doButtons()
        If bStart Then
            With Me
                .CommandButton1.Enabled = False
                .CommandButton2.Enabled = True
                .CommandButton2.Visible = True
                .CommandButton3.Enabled = True
            End With
        Else: With Me
                .CommandButton1.Enabled = True
                .CommandButton2.Enabled = False
                .CommandButton2.Visible = False
                .CommandButton3.Enabled = False
            End With
        End If
    End Sub
    You could make it slightly more efficient with less code

    Option Explicit
    Dim bStart     As Boolean
    Private Sub CommandButton1_Click()
        bStart = True
        doButtons
    End Sub
    
    Private Sub CommandButton3_Click()
        bStart = False
        doButtons
    End Sub
    
    Sub doButtons()
       
            With Me
                .CommandButton1.Enabled = Not bStart
                .CommandButton2.Enabled = bStart
                .CommandButton2.Visible = bStart
                .CommandButton3.Enabled = bStart
            End With
       
    End Sub
    There's not really any need to set a Boolean to determine what to do because the button status is a Boolean. This code does exactly what you want simply by changing the buttons' status

    Option Explicit
    
    Private Sub CommandButton1_Click()
        doButtons
    End Sub
    
    Private Sub CommandButton3_Click()
        doButtons
    End Sub
    
    Sub doButtons()
        With Me
            .CommandButton1.Enabled = Not .CommandButton1.Enabled
            .CommandButton2.Enabled = Not .CommandButton2.Enabled
            .CommandButton2.Visible = Not .CommandButton2.Visible
            .CommandButton3.Enabled = Not .CommandButton3.Enabled
        End With
    End Sub
    Last edited by royUK; 04-05-2008 at 03:15 AM.
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

+ 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.2.0