Results 1 to 2 of 2

Thread: This "IF THEN" thing is driving me %$#@#!@!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 1999
    Posts
    11

    Post

    I’ve been taking the “Visual Basic 6.0 step by step” learning addition course, and thought I would branch off on my own, and try a little code using the “If Then” statement. Using what looked like very simple code, I kept getting this annoying “End without If “ message. I finally removed the “End If” statement and the sample code ran fine. The book doesn’t seem to address when to use the “End If” statement. So consequently, I never figured out why this was happening. So here I am, a couple of weeks later, I’m trying to do a tutorial on “Visual Basic and Data Bases”, as soon as I saw the block of code with the “If Then.. End If” statement I knew I was in trouble, and sure enough I had problems.
    Can anybody help me out here??
    Here is the block of code from the tutorial:

    Dim S as string, I as Integer
    For I = 0 to 4
    If txtInput(I).text = “” Then _
    MsgBox “Each box must have an entry!”, vbInformation + vbOKOnly, “Error”
    Exit Sub
    End If
    Next I
    End Sub

  2. #2
    Member
    Join Date
    Feb 1999
    Location
    NY
    Posts
    45

    Post

    It seems you got some things mixed up.

    When you use the _ or see it, it means that its taking whatever is on the very next line, and tacking it on to the current line

    Such as:
    If _
    a = _
    1 then _

    is the equivalent of:
    If a = 1 then

    By using:
    If txtInput(I).text = “” Then _
    MsgBox “Each box must have an entry!”, vbInformation + vbOKOnly, “Error”
    Exit Sub
    End If

    You are saying
    If txtInput(I).text = "" Then MsgBox “Each box must have an entry!”, vbInformation + vbOKOnly, “Error”
    Exit Sub
    End If

    If you copied it from a book, either the book is wrong, or look again.
    A little note on if thens, and this'll explain the error. A single line if then, is just that, a single line, and it requires no End If, in fact if you gave it an End If, it'll give you "End If without block If". Likewise if you had an If without and End If, guess what the error would be. You used a single line if then by accident using the _, and you made VB think you are using a single-line if then, and it knows that doesn't need an End If. In any case, the correct code is:

    Dim S as string, I as Integer
    For I = 0 to 4
    If txtInput(I).text = “” Then
    MsgBox “Each box must have an entry!”, vbInformation + vbOKOnly, “Error”
    Exit Sub
    End If
    Next I
    End Sub

    Thats a multi line if then, and it does require an End If, or else VB wouldn't know where it ended.

    ------------------
    -Mystiq

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width