End if statements

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

Join Date: Mar 2004
Posts: 13
Reputation: nobody2ph is an unknown quantity at this point 
Solved Threads: 0
nobody2ph's Avatar
nobody2ph nobody2ph is offline Offline
Newbie Poster

End if statements

 
0
  #1
Jun 7th, 2006
hi guys, i know this might look like a n00b's homework assignment - but it aint, im studying VB for myself.

Im trying to do this tutorial over here, cuz everybody said it's good
http://www.profsr.com/vb/vbless04.htm

Prob is when i try to press the confirm button, error message pops up saying "end if without block if"? I've tried organizing my code but that doesn't seem to work... I don't get it, am I using incorrect syntax?

Here's the code for the confirm button below
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub confirm_Click()
  2. Dim hoursbox As Integer
  3. Dim deductbox As Integer
  4. Dim rate As Integer
  5. Dim grosalary As String
  6. Dim netsalary As String
  7.  
  8. If op1.Value = False _
  9. And op2.Value = False _
  10. And op3.Value = False Then
  11. grosalary = "invalid" And netsalary = "invalid"
  12. End If
  13.  
  14. If op1.Value = True Then rate = 10
  15. End If
  16.  
  17. If op2.Value = True Then rate = 20
  18. End If
  19.  
  20. If op3.Value = True Then rate = 30
  21. End If
  22.  
  23. grosalary = rate * hoursbox
  24. netsalary = grosalary - deductbox
  25.  
  26. End Sub

Thanks ^^
Last edited by Comatose; Jun 7th, 2006 at 6:31 am.
Nobody2ph is an agent of the Forgeway Network http://forgeway.cjb.net/
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 37
Reputation: ramiljoaquin is an unknown quantity at this point 
Solved Threads: 2
ramiljoaquin ramiljoaquin is offline Offline
Light Poster

Re: End if statements

 
0
  #2
Jun 7th, 2006
try this,,,

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2.  
  3. Dim hoursbox As Integer
  4. Dim deductbox As Integer
  5. Dim rate As Integer
  6. Dim grosalary As String
  7. Dim netsalary As String
  8.  
  9. If op1.Value = False _
  10. And op2.Value = False _
  11. And op3.Value = False Then
  12.  
  13. grosalary = "invalid"
  14. netsalary = "invalid"
  15.  
  16. End If
  17.  
  18. If op1.Value = True Then rate = 10
  19.  
  20. If op2.Value = True Then rate = 20
  21.  
  22. If op3.Value = True Then rate = 30
  23.  
  24. grosalary = rate * hoursbox
  25. netsalary = grosalary - deductbox
  26.  
  27. End Sub
Last edited by Comatose; Jun 7th, 2006 at 6:31 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: End if statements

 
0
  #3
Jun 7th, 2006
One problem that a lot of people don't understand is that an if statement has 2 different syntax. The only time you have to "end if" an if statement, is when the if statement is on more than 1 line (or if it does more than one thing). So for example, if I want to make this program change the value of a variable, display a msgbox and leave the sub, I would do this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. somevariable = 1
  2. if x = 5 then
  3. somevariable = 10
  4. msgbox "changed somevariable to 10"
  5. exit sub
  6. end if
However, let's say all I want to do is change the variable, and that's it.... just change the variable and move on, if x = 5:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. somevariable = 1
  2. if x = 5 then somevariable = 10
And that will work just fine..... in fact, it will give an error if you put a end if..... without have a "block" of code.

Another thing to keep in mind, is that it really isn't a good practice to put code that should be on 1 line, in more than 1 line. For example, the underscores used in your first if block. While VB allows you to do this (and it's supposed to be for readability) you shouldn't. Each line should represent a different purpose..... it keeps things in order. If you are comfortable programmang with the _, then keep doing it, but be aware on team projects and larger scale apps, you will kick yourself in the rump for it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 13
Reputation: nobody2ph is an unknown quantity at this point 
Solved Threads: 0
nobody2ph's Avatar
nobody2ph nobody2ph is offline Offline
Newbie Poster

Re: End if statements

 
0
  #4
Jun 7th, 2006
thanks guys , i understand a lot better now ^^
Nobody2ph is an agent of the Forgeway Network http://forgeway.cjb.net/
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 37
Reputation: ramiljoaquin is an unknown quantity at this point 
Solved Threads: 2
ramiljoaquin ramiljoaquin is offline Offline
Light Poster

Re: End if statements

 
0
  #5
Jun 8th, 2006
your welcome
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 85
Reputation: Dell XPS is an unknown quantity at this point 
Solved Threads: 0
Dell XPS Dell XPS is offline Offline
Junior Poster in Training

Re: End if statements

 
0
  #6
Sep 14th, 2007
sorry wrong post
Last edited by Dell XPS; Sep 14th, 2007 at 12:42 pm.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
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