Alamb
Alamb

Reputation: 137

If Else Statement is giving an error that I'm missing an End If statement

I'm hoping for another set of eyes to look over my code. I'm writing a relatively big If...Else... statement. It appears that everything is coded properly. I've stepped through the entire script but one spot keeps giving me an "Expected 'End If'" When it seems to me that I don't need that.

Simply put, I'm trying to say: If blah, do these for things. Else If blah, only do one of those things.

Here's the block of code:

If(sLogFile <> "" AND nPing <> 1) Then
    pingStatus = 1

    If(findTag(sLogFile, daRunning) = 1) Then
        daStatus = 1
    Else If(findTag(sLogFile, daNotRunning) = 1) Then
        daStatus = 2
    Else
        daStatus = 3
    End If

    If(daStatus = 1 AND findTag(sLogFile, daDataFlowing) = 1) Then
        daFlowStatus = 1
    Else If(daStatus = 1 AND findTag(sLogFile, daDataNotFlowing) = 1) Then
        daFlowStatus = 2
    Else If(daStatus = 1 AND findTag(sLogFile, daDataUnchecked) = 1) Then
        daFlowStatus = 3
    Else If(daStatus <> 1) Then
        daFlowStatus = 4
    End If

    If(findTag(sLogFile, aeRunning) = 1) Then
        aeStatus = 1
    Else If(findTag(sLogFile, aeNotRunning) = 1) Then
        aeStatus = 2
    Else
        aeStatus = 3
    End If

    If(findTag(sLogFile, aeDataUnchecked) = 1) Then
        aeFlowStatus = 2
    Else If(findTag(sLogFile, aeDataExecutionError) = 1) Then
        aeFlowStatus = 3
    Else If(findTag(sLogFile, aeDataConnectionError) = 1) Then
        aeFlowStatus = 4
    Else If(findTag(sLogFile, aeTimeStamp) = 1) Then
        location = InStr(sLogFile, aeTimeStamp)
        leftTrimmedString = LTrim(Mid(sLogFile, (location - 2)))
        location = InstrRev(leftTrimmedString, ":")
        dateString = Trim(Mid(leftTrimmedString, 1, (location + 5)))
        timeDiff = DateDiff("h", dateString, Now)
        If(timeDiff > 5) Then
            aeFlowStatus = 5
        Else
            aeFlowStatus = 1
        End If
    End If

Else If(nPing = 1) Then
    pingStatus = 2

    If(findTag(sLogFile, aeDataUnchecked) = 1) Then
        aeFlowStatus = 2
    Else If(findTag(sLogFile, aeDataExecutionError) = 1) Then
        aeFlowStatus = 3
    Else If(findTag(sLogFile, aeDataConnectionError) = 1) Then
        aeFlowStatus = 4
    Else If(findTag(sLogFile, aeTimeStamp) = 1) Then
        location = InStr(sLogFile, aeTimeStamp)
        leftTrimmedString = LTrim(Mid(sLogFile, (location - 2)))
        location = InstrRev(leftTrimmedString, ":")
        dateString = Trim(Mid(leftTrimmedString, 1, (location + 5)))
        timeDiff = DateDiff("h", dateString, Now)
        If(timeDiff > 5) Then
            aeFlowStatus = 5
        Else
            aeFlowStatus = 1
        End If
    End If
End If

The error keeps happening at the main Else If about 2/3 of the way down (Else If(nPing = 1) Then). I'm getting the error on that line.

I've tried separating the block into two blocks by putting an End If above the Else IF, then changing the Else If to an If. It works when I do that but I don't really want two if statements.

So, did I mess up, or is there a problem with my interpreter?

Upvotes: 0

Views: 2653

Answers (2)

rory.ap
rory.ap

Reputation: 35270

The reason is that you should be using ElseIf not Else If. See the difference?

Now, the bigger issue is that you will always have trouble figuring out errors with such a huge chunk of "spaghetti code". Consider breaking your code up into small re-usable methods. That way, you'll be able to figure out where the problem lies more-easily. Not to mention the fact that it hurts to even look at all those if blocks all crammed together.

Upvotes: 2

matthew_b
matthew_b

Reputation: 759

In Visual Basic the "else if" statement is written ElseIf not Else If. I think that's your problem.

Upvotes: 3

Related Questions