Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    If statement termination (VB/VBA)

    Has anyone else noticed this?

    I have an If statement, depending on data type, does a comparison.

    It goes something like this.

    If myDataType = "D" And (cDate(someField1) <> cDate(someField2)) then

    The problem is this. The someField1 and someField2 are not dates and myDataType is not "D". I would expect the If statement to terminate when it evaluates myDataType not being D but it doesn't appear to do so. I get a Type Mismatch error pointing to the someField1 or someField2 variables.

    OK, I know how to restructure my code to get around this but it is annoying to have to do that to say the least.

    Any suggestions anybody? (Ones that can be printed anyways).

    Regards,

    Kevin Bell

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If statement termination (VB/VBA)

    The whole statement gets evaluated, not just the first condition. If you have a condition that might throw an error like that, you'll have to nest it inside the first test. I'm sure this is what you meant by knowing how to program around it but this is what I mean:

    If myDataType = "D" Then
    If (cDate(someField1)<>cDate(someField2) Then
    .....

    As far as I recall, VBA has always worked this way.
    Charlotte

  3. #3
    Star Lounger
    Join Date
    Aug 2001
    Location
    GA
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If statement termination (VB/VBA)

    If you are getting a Type Mismatch error pointing to the someField1 or someField2 variables. First test them to ensure they are valid dates before you try to convert them to dates. If the variables have Alpha characters, or a NULL value when you call CDATE() function it throws the Type MisMatch error at you.

    The construction of your IF...END IF depends a lot on the logic of your program but I'll assume that It's important to first ensure that the two date variables are valid dates so something like this will work.

    If IsDate(someField1) AND IsDate(someField2) THEN
    ' We can check the dataType and do the date comparison
    If (myDataType = "D") AND (cDate(someField1) <> cDate(someField2)) THEN
    'Do stuff here because dates are different and we have "D"
    ELSE
    'Do other stuff when dates are the same or we don't have "D"
    ENDIF
    ELSE
    'One or Both variable are not dates, so we'll do something else here.
    END IF

  4. #4
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If statement termination (VB/VBA)

    Thanks for the posts. I worked around the error just as you both said but it's still anoying to have to code differently because VB/VBA doesn't terminate the If.
    All the other languages I have used, either on the PC or on various other computers have terminated an If statement as soon as it becomes False. I do believe that this is considered the 'norm'. I'll have to ask Norm and see what He says.

    You live and learn.

    Cheers,

    Kevin Bell

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If statement termination (VB/VBA)

    All I can say is that VB/VBA *never* has behaved that way, so it isn't likely to start in the near future. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: If statement termination (VB/VBA)

    You will only find that behavior in languages that have optimizing compilers. VB/VBA does not have an optimizer.
    Legare Coleman

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: If statement termination (VB/VBA)

    If you go much beyond the "near future", it won't be VB/VBA anyway. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    I wonder how it's handled in VB.Net?

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If statement termination (VB/VBA)

    I haven't had time to look and see, but there isn't any VBA.Net yet anyhow. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: If statement termination (VB/VBA)

    The language VB.Net is here already, and we can safely assume that the language will be the same when VBA.Net does get here (next year, you figure?).

    Anyway Big Kev gets his wish - according to VB.Net Language in a Nutshell:

    "In performing logical operations, VB . Net, unlike VB 6, uses conditional short-circuiting.... Short-circuiting can occur in logical And operations when the first operand evaluates to False, as well as in logical Or operations when the first operand evaluates to True."

    Gary

  10. #10
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: If statement termination (VB/VBA)

    Woooooo hoooooooo.

    Thanks Gary.

    Now all I have to do is justify shelling out money for a language that has a feature that I can code around.

    I think I'll stick to If ... Then ... Else, it's cheaper.

    Cheers,

    Kevin Bell

Posting Permissions

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