I'm trying to write a game for fun. I've been able to get some lines of code from other friends who have been able to help. But they couldn't help me with this. I'm trying to write the code to where, when you click on the button it should change the caption one time. Thus you click on the button and it starts at 1 and stays at one. However it instead runs through all of the If commands and instead of stopping at 1, it proceeds all the way to 3. Here is a copy of the code:

Private Sub Tornado_Click()
If Me.TL.Caption = "" Then
Me.TL.Caption = 1
End If

If Me.TL.Caption = 1 Then
Me.TL.Caption = 2
End If

If Me.TL.Caption = 2 Then
Me.TL.Caption = 3
End If
End Sub

Recommended Answers

All 3 Replies

After it's changed to 1, it goes on to the next if statement(if = 1), Since now it is =1 it does that if statement and continues to the next etc.
Try

Select Case Me.Tl.Caption
Case ""
me.tl.caption = 1
Case 1
me.tl.caption = 2
case 2
me.tl.caption = 3
End select

Or change your if statement to the following

If me.tl.caption = "" then
me.tl.caption = 1
elseif me.tl.caption = 1 then
me.tl.caption =2
elseif me.tl.caption =2 then
me.tl.caption =3
end if

this way it does only one of the statements

Thank you very much. That worked. The one I needed was the ElseIf. Thus you could click the button and it applies once each time its clicked. Im sure it was simple for you guys, but I couldn't tell how to terminate it from continuing the If statement. I can setup such things like a random pick between a set of numbers and display that, like in a roll of the dice but just couldn't figure that out. Thanks

every if put exit sub before end if
example:

Private Sub Tornado_Click()
If Me.TL.Caption = "" Then
Me.TL.Caption = 1
exit sub
End If 

If Me.TL.Caption = 1 Then
Me.TL.Caption = 2
exit sub
End If 

If Me.TL.Caption = 2 Then
Me.TL.Caption = 3
End If
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.