943,546 Members | Top Members by Rank

Ad:
Aug 13th, 2007
0

Needs help with If statement

Expand Post »
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
Similar Threads
Reputation Points: 2
Solved Threads: 0
Newbie Poster
venomocity is offline Offline
16 posts
since Aug 2007
Aug 14th, 2007
0

Re: Needs help with If statement

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
Reputation Points: 10
Solved Threads: 16
Posting Whiz in Training
plusplus is offline Offline
207 posts
since Jul 2007
Aug 14th, 2007
0

Re: Needs help with If statement

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
Last edited by venomocity; Aug 14th, 2007 at 2:35 pm.
Reputation Points: 2
Solved Threads: 0
Newbie Poster
venomocity is offline Offline
16 posts
since Aug 2007
Aug 16th, 2007
0

Re: Needs help with If statement

every if put exit sub before end if
example:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Tornado_Click()
  2. If Me.TL.Caption = "" Then
  3. Me.TL.Caption = 1
  4. exit sub
  5. End If
  6.  
  7. If Me.TL.Caption = 1 Then
  8. Me.TL.Caption = 2
  9. exit sub
  10. End If
  11.  
  12. If Me.TL.Caption = 2 Then
  13. Me.TL.Caption = 3
  14. End If
  15. End Sub
Reputation Points: 11
Solved Threads: 49
Posting Whiz
jireh is offline Offline
316 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Updating Combo Box
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: cpu/ram usage





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC