943,786 Members | Top Members by Rank

Ad:
Jun 7th, 2006
0

End if statements

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nobody2ph is offline Offline
15 posts
since Mar 2004
Jun 7th, 2006
0

Re: End if statements

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.
Reputation Points: 10
Solved Threads: 2
Light Poster
ramiljoaquin is offline Offline
37 posts
since Jun 2006
Jun 7th, 2006
0

Re: End if statements

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 7th, 2006
0

Re: End if statements

thanks guys , i understand a lot better now ^^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nobody2ph is offline Offline
15 posts
since Mar 2004
Jun 8th, 2006
0

Re: End if statements

your welcome
Reputation Points: 10
Solved Threads: 2
Light Poster
ramiljoaquin is offline Offline
37 posts
since Jun 2006
Sep 14th, 2007
0

Re: End if statements

sorry wrong post
Last edited by Dell XPS; Sep 14th, 2007 at 12:42 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Dell XPS is offline Offline
85 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Draw text on JPEG
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: WaitForEvent if then argument





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC