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 ^^

Recommended Answers

All 5 Replies

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

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.

thanks guys , i understand a lot better now ^^

sorry wrong post

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.