| | |
Needs help with If statement
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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
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
•
•
Join Date: Jul 2007
Posts: 193
Reputation:
Solved Threads: 16
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
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
Last edited by venomocity; Aug 14th, 2007 at 2:35 pm.
every if put exit sub before end if
example:
example:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
A conclusion is the place where you got tired of thinking. http://www.martin2k.co.uk/forums/index.php?showforum=4
http://www.a1vbcode.com/a1vbcode/vbforums/Forum3-1.aspx
http://www.developerfusion.co.uk/for...orum&ForumID=4
![]() |
Similar Threads
- missing return statement (Java)
- MySQL LIKE statement (MySQL)
- loop in main function to an "if" statement (C++)
- Switch Case Statement (Java)
- run sql statement in asp (ASP)
- switch/case statement (C++)
- change statement (JSP)
- Reading MSWord Document through an ASP Statement (ASP)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Updating Combo Box
- Next Thread: cpu/ram usage
Views: 1017 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Visual Basic 4 / 5 / 6
* 6 429 2007 access activex add age append application basic beginner birth bmp c++ calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver struct subroutine table tags textbox timer urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





