Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

IF statement only partially working. Why?!

Hello.

I have put the private sub together to do input calculated values into fields (I'm not using calculated fields on purpose).

The VAT calculation works fine, and so does the "else" part of the IF statement. However, the first statement (immediately after THEN) is not working.

Help please!!!!

Below is the code:

Dim RedCost As Currency
RedCost = CostOption - 500

VAT = CostOption * 17.5 / 100

If CostOption > 500 Then
FundCont = (RedCost / 2) + 500 And CompCont = RedCost / 2
Else
FundCont = 500 And CompCont = 0
End If

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : Hello.
    :
    : I have put the private sub together to do input calculated values
    : into fields (I'm not using calculated fields on purpose).
    :
    : The VAT calculation works fine, and so does the "else" part of the
    : IF statement. However, the first statement (immediately after THEN)
    : is not working.
    :
    : Help please!!!!
    :
    : Below is the code:
    :
    : Dim RedCost As Currency
    : RedCost = CostOption - 500
    :
    : VAT = CostOption * 17.5 / 100
    :
    : If CostOption > 500 Then
    : FundCont = (RedCost / 2) + 500 And CompCont = RedCost / 2
    : Else
    : FundCont = 500 And CompCont = 0
    : End If
    :

    The part under the If and also under the Else is not doing what you think it's doing. I'll take the Else-line to explain to you what you have actually told VB to do:
    [code]
    'This is the line you have
    FundCont = 500 And CompCont = 0
    'What you *think* it does is the following two lines:
    FundCont = 500
    CompCont = 0

    'What it actually does is:
    FundCont = (500 And CompCont) = 0
    'Which makes 'And' a 'Binary And' of 500 = 111111010b and CompCont in binary
    'This makes the =0 not an assignment, but an equality check, returning true or false, so this is actually what you've put down for that one line:
    If (500 And CompCont) = 0 Then
    FundCont = True
    Else
    FundCont = False
    End If
    [/code]

    So the solution is to not use And, but simply place the multiple commands on separate lines in the If-block.
    Good luck
    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.