Sphyrna
Sphyrna

Reputation: 11

VBA simple code

Private Sub cmdplay_click()

    LblGameName.Caption = "Hello! And welcome to Unicorn Adventure Extream! Are you excited?"
    CmdExit.Caption = "no"
    CmdPlay.Caption = "yes"
    If CmdPlay = True Then
        LblGameName.Caption = "As you should be!! Welcome to Unicopitopia! Will you please enjoy your stay?"
    ElseIf CmdExit = True Then
        LblGameName.Caption = "You have angered the unicorn!! She ripped out your heart and devoured it! Bad Luck! Much Blood!"
    End If

End Sub

I dont understand why my label isn't changing when i press yes or no?

Upvotes: 0

Views: 93

Answers (1)

peege
peege

Reputation: 2477

There are a few issues with your example.

You will want to have your buttons named and have the text in them stay the same. One could be cmdYes, and the other cmdExit. Then have click events associated with those buttons. To get that to happen, just double click on the button in the Form Object Designer mode, and it will bring up the code and generate a click event.

You definitely have things misnamed, according to your earlier naming.

I would have the Lbl you have named something like txtMessage, and have it as a locked textbox. You can set the background color of the textbox to match the UserForm color. Then just set the text of it to update with whatever printout you are trying to achieve.

Private Sub UserForm_Activate()

    txtMessage.Text =  "Hello! And welcome to Unicorn Adventure Extreme! Are you excited?"

End Sub 

Then have separate events for your buttons.

Private Sub cmdExit_click()

    Dim exitMessage As String

    exitMessage = "You have angered the unicorn!! She ripped out your heart and devoured it! Bad Luck! Much Blood!"
    txtMessage.Text = exitMessage

    MsgBox("Game Over, Goodbye") ' or whatever else you want to have happen.  
    Unload Me      'You could unload the form here.

End Sub

A separate event for each button.

Private Sub cmdYes_click()

    Dim YesMessage As String

    YesMessage = "As you should be!! Welcome to Unicopitopia! Will you please enjoy your stay?"
    txtMessage.Text = YesMessage

    'Call some other subroutine that launches your game.
End Sub

Upvotes: 1

Related Questions