Archived

This topic is now archived and is closed to further replies.

Martyn

Urgent: Ending An If Statement

Recommended Posts

Martyn    122
Hello, I am working on this college project, I need a little help with ending this weird loop/if statement, I am generally new to programming. The thing is, if all of the text boxes have values in them I don''t want the errh/msg boxes to continue popping up, and here is my current code for the command button if it helps: Private Sub cmdCalculate_Click() Dim Cal1 As Integer Dim Cal2 As Integer Dim Cal3 As Integer Dim Cal4 As Integer Dim Cal5 As Currency Dim Cal6 As Integer Dim Cal7 As Currency Dim a As Currency Dim b As Currency Dim c As Currency Dim d As Currency Dim e As Currency Dim f As Currency Dim g As Currency Dim h As Currency Dim i As Currency Dim j As Currency a = txtConsCost.Text g = txtGSCost2.Text h = txtFSCost2.Text i = txtFTCost2.Text j = txtWTCost2.Text If a = "0" Then GoTo Errh1 If g = "0" Then GoTo Errh2 If h = "0" Then GoTo Errh3 If i = "0" Then GoTo Errh4 If j = "0" Then GoTo Errh5 b = txtGSCost.Text c = txtWTCost.Text d = txtFSCost.Text e = txtFTCost.Text f = txtAHCost.Text Cal: Cal1 = a * b Cal2 = a * c Cal3 = a * d Cal4 = a * e Cal5 = a + f + Cal1 + Cal2 + Cal3 + Cal4 Cal6 = Cal5 / 10 txtPrice1.Text = Format(Cal5, "currency") txtPrice2.Text = Format(Cal5 * 1.175, "currency") txtPrice3.Text = Format(Cal5 / 10, "currency") Cal7 = Cal5 + Cal6 txtPrice4.Text = Format(Cal7 * 1.175, "currency") Errh1: MsgBox "Select Conservatory Type" Errh2: MsgBox "Select Glass Style" Errh3: MsgBox "Select Floor Size" Errh4: MsgBox "Select Floor Type" Errh5: MsgBox "Select Wood Type" End Sub

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I''m not that much into VB, but I think what you need to do is insert the line "Exit Sub" after each error message-box so that processing in the function is stopped., ie the last few lines of your code should be:

.
.
.

Errh1:
MsgBox "Select Conservatory Type"
Exit Sub
Errh2:
MsgBox "Select Glass Style"
Exit Sub
Errh3:
MsgBox "Select Floor Size"
Exit Sub
Errh4:
MsgBox "Select Floor Type"
Exit Sub
Errh5:
MsgBox "Select Wood Type"
Exit Sub

End Sub


Actually, I think that you need to put an "Exit Sub" statement in after the last line of calculation, otherwise it will try to bring up the first message box (even though it shouldn''t) - your first bug

Share this post


Link to post
Share on other sites
LousyPhreak    133
Hmm basic has been awhile but ill give it a try:


Private Sub cmdCalculate_Click()

Dim Cal1 As Integer
Dim Cal2 As Integer
Dim Cal3 As Integer
Dim Cal4 As Integer
Dim Cal5 As Currency
Dim Cal6 As Integer
Dim Cal7 As Currency
Dim a As Currency
Dim b As Currency
Dim c As Currency
Dim d As Currency
Dim e As Currency
Dim f As Currency
Dim g As Currency
Dim h As Currency
Dim i As Currency
Dim j As Currency

a = txtConsCost.Text
g = txtGSCost2.Text
h = txtFSCost2.Text
i = txtFTCost2.Text
j = txtWTCost2.Text

If a = "0" Then
MsgBox "Select Conservatory Type"
ElseIf g = "0" Then
MsgBox "Select Glass Style"
ElseIf h = "0" Then
MsgBox "Select Floor Size"
ElseIf i = "0" Then
MsgBox "Select Floor Type"
ElseIf j = "0" Then
MsgBox "Select Wood Type"
Else Then

b = txtGSCost.Text
c = txtWTCost.Text
d = txtFSCost.Text
e = txtFTCost.Text
f = txtAHCost.Text

Cal1 = a * b
Cal2 = a * c
Cal3 = a * d
Cal4 = a * e
Cal5 = a + f + Cal1 + Cal2 + Cal3 + Cal4
Cal6 = Cal5 / 10
txtPrice1.Text = Format(Cal5, "currency")
txtPrice2.Text = Format(Cal5 * 1.175, "currency")
txtPrice3.Text = Format(Cal5 / 10, "currency")
Cal7 = Cal5 + Cal6
txtPrice4.Text = Format(Cal7 * 1.175, "currency")

EndIf

End Sub


Some notes though:
stay away from goto''s they make your code unreadable (but *can* be helpful if used correctly, but this is not even 1 in a million)
If''s can be ended with an EndIf, if you need another clause just add an ElseIf instead (note that the If as a whole still needs to be terminated, see above if what i say makes no sense)

Share this post


Link to post
Share on other sites
Promit    13246
I should point out that while you''re writing in Visual Basic, your actual coding style is basically old-style BASIC.

This is a BAD thing. You shold be able to structure your program more effectively with the use of extra methods, control structures, etc. But since you''re new, I''ll give you a break, for now

Share this post


Link to post
Share on other sites