954,148 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

End if statements

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

Private Sub confirm_Click()
 Dim hoursbox As Integer
 Dim deductbox As Integer
 Dim rate As Integer
 Dim grosalary As String
 Dim netsalary As String
 
If op1.Value = False _
   And op2.Value = False _
   And op3.Value = False Then
  grosalary = "invalid" And netsalary = "invalid"
End If
 
If op1.Value = True Then rate = 10
End If
 
If op2.Value = True Then rate = 20
End If
 
If op3.Value = True Then rate = 30
End If
 
grosalary = rate * hoursbox
netsalary = grosalary - deductbox
 
End Sub


Thanks ^^

nobody2ph
Newbie Poster
15 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

try this,,, :)

Private Sub Command1_Click()
    
    Dim hoursbox As Integer
    Dim deductbox As Integer
    Dim rate As Integer
    Dim grosalary As String
    Dim netsalary As String
    
    If op1.Value = False _
    And op2.Value = False _
    And op3.Value = False Then
    
        grosalary = "invalid"
        netsalary = "invalid"
    
    End If
    
    If op1.Value = True Then rate = 10
    
    If op2.Value = True Then rate = 20
 
    If op3.Value = True Then rate = 30
    
    grosalary = rate * hoursbox
    netsalary = grosalary - deductbox

End Sub
ramiljoaquin
Light Poster
37 posts since Jun 2006
Reputation Points: 10
Solved Threads: 2
 

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:

somevariable = 1
if x = 5 then
     somevariable = 10
     msgbox "changed somevariable to 10"
     exit sub
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:

somevariable = 1
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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

thanks guys , i understand a lot better now ^^

nobody2ph
Newbie Poster
15 posts since Mar 2004
Reputation Points: 10
Solved Threads: 0
 

your welcome :D

ramiljoaquin
Light Poster
37 posts since Jun 2006
Reputation Points: 10
Solved Threads: 2
 

sorry wrong post

Dell XPS
Junior Poster in Training
85 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You