• If statement termination (VB/VBA)

    Author
    Topic
    #365451

    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

    Viewing 1 reply thread
    Author
    Replies
    • #564151

      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.

    • #564290

      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

      • #564323

        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

        • #564349

          All I can say is that VB/VBA *never* has behaved that way, so it isn’t likely to start in the near future. shrug

          • #564541

            If you go much beyond the “near future”, it won’t be VB/VBA anyway. grin
            I wonder how it’s handled in VB.Net?

            • #564568

              I haven’t had time to look and see, but there isn’t any VBA.Net yet anyhow. shrug

            • #564630

              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

            • #564811

              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

        • #564474

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

    Viewing 1 reply thread
    Reply To: If statement termination (VB/VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: