Needs help with If statement

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2007
Posts: 15
Reputation: venomocity is an unknown quantity at this point 
Solved Threads: 0
venomocity's Avatar
venomocity venomocity is offline Offline
Newbie Poster

Needs help with If statement

 
0
  #1
Aug 13th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 192
Reputation: plusplus is an unknown quantity at this point 
Solved Threads: 16
plusplus plusplus is offline Offline
Junior Poster

Re: Needs help with If statement

 
0
  #2
Aug 14th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 15
Reputation: venomocity is an unknown quantity at this point 
Solved Threads: 0
venomocity's Avatar
venomocity venomocity is offline Offline
Newbie Poster

Re: Needs help with If statement

 
0
  #3
Aug 14th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 300
Reputation: jireh is an unknown quantity at this point 
Solved Threads: 43
jireh's Avatar
jireh jireh is offline Offline
Posting Whiz

Re: Needs help with If statement

 
0
  #4
Aug 16th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC