Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Why am i getting this error??

Options
  • 28-01-2006 1:10pm
    #1
    Registered Users Posts: 618 ✭✭✭


    Ok i keep on getting an else without if error here and i cannot understand it anyone got any ideas

    If Source.Name = imgten And Val(frmstock.txttencents) = 0 Then
    lblerror.Caption = "you have no more ten cents left"
    ElseIf Source.Name = "imgten" Then txtamount = Val(txtamount.Text) + 0.1
    End If


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    try moving "txtamount = Val(txtamount.Text) + 0.1" to the next line?

    What is this ? VB?


  • Registered Users Posts: 618 ✭✭✭CrazySka


    yeah sorry should have said its VB
    its wierd though ive gone back over previous projects ive done and the syntax seems fine, ive just reinstalled vb the other day, is it possible ive got some option set wrong or something???


  • Registered Users Posts: 199 ✭✭DecTenToo


    Might want to structure that better

    If Source.Name = "imgten" then
    if Val(frmstock.txttencents) = 0 Then
    lblerror.Caption = "you have no more ten cents left"
    Else
    txtamount = Val(txtamount.Text) + 0.1
    end if
    End If


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Generally the AND would be better then a nested IF statement. Overuse of nested IF statements can cause your code to become hard to read and debug.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    john_dub wrote:
    If Source.Name = imgten And Val(frmstock.txttencents) = 0 Then
    lblerror.Caption = "you have no more ten cents left"
    ElseIf Source.Name = "imgten" Then txtamount = Val(txtamount.Text) + 0.1
    End If

    Did you cut and paste this code? I've highlighted an obvious problem although this shouldn't give you an else without if error. If you didn't can you c&p your code (the entire function) and c&p the error message?


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    john_dub wrote:
    Ok i keep on getting an else without if error here and i cannot understand it anyone got any ideas

    I'm guessing that what you've typed in here is not *exactly* what your code is.

    See, I reckon that what you have is the following :

    if <condition> then <action>
    elseif <condition> then <action>
    end if

    This structure is invalid in Visual Basic.

    For better or for worse, VB allows the following :

    if <condition> then <action>

    and

    if <condition then
    <action>
    End If


    Note - my decision to put the action on a new line means I require an End If. If I don't put the action on a new line, then I don't require and end-if. Indeed, I don't just not require one....an end-if is invalid in this second example.

    Now....you want an else-if included in there. This is done differently for each of the two cases above:

    if <condition> then <action> else if <other condition> then <other action>

    and

    if <condition then
    <action>
    else if <other codition> then
    <other action>
    End If


    Your code is some hybrid of these - you have your if written as though it was the first of my 2 examples, and the 'else if' written as though it was the second. This is invalid.

    Also, as Phil pointed out, you have almost definitely have a seperate error, where imgten is treated once as a string value and once as a variable.


  • Registered Users Posts: 618 ✭✭✭CrazySka


    Ok i think i have it sorted now, cheers for all your help everyone, project time is a bas**** ya know.


Advertisement