#### Archived

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

# Urgent: Ending An If Statement

This topic is 5418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 SubErrh2:MsgBox "Select Glass Style"Exit SubErrh3:MsgBox "Select Floor Size"Exit SubErrh4:MsgBox "Select Floor Type"Exit SubErrh5:MsgBox "Select Wood Type"Exit SubEnd 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 on other sites
Thanks for the speedy reponse, it works great now. Cheers.

##### Share on other sites
Hmm basic has been awhile but ill give it a try:

Private Sub cmdCalculate_Click()Dim Cal1 As IntegerDim Cal2 As IntegerDim Cal3 As IntegerDim Cal4 As IntegerDim Cal5 As CurrencyDim Cal6 As IntegerDim Cal7 As CurrencyDim a As CurrencyDim b As CurrencyDim c As CurrencyDim d As CurrencyDim e As CurrencyDim f As CurrencyDim g As CurrencyDim h As CurrencyDim i As CurrencyDim j As Currencya = txtConsCost.Textg = txtGSCost2.Texth = txtFSCost2.Texti = txtFTCost2.Textj = txtWTCost2.TextIf 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")EndIfEnd 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 on other sites
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

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×