• TestFormula functions

    By Eric H Romo 2 decades ago

    I added in a TestFormula method as a more generic method to test a formula string (parameter 1)



    I also created a Check method and CheckUntil method which loop through the string-pairs (parameter 1) and return a boolean based on the flag (parameter 2)


    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
    '<br/>
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
    Function TestFormula(Formula As String) As Boolean<br/>
        On Error Goto processError<br/>
        <br/>
        Dim lastFormula As String<br/>
        Dim replaceString As String<br/>
        Dim result As Variant<br/>
        Dim t As String<br/>
        <br/>
        If Me.IsString Then<br/>
            replaceString = |&quot;| &amp; fieldValue &amp; |&quot;|<br/>
        Elseif Me.IsNumber Then<br/>
            replaceString = fieldValue<br/>
        Else<br/>
            replaceString = |[| &amp; fieldValue &amp; |]|<br/>
        End If<br/>
        <br/>
        lastFormula = Formula<br/>
        t = Replace(Formula, |@ThisValue|, replaceString)<br/>
        result = Evaluate(t, doc)<br/>
        TestFormula = Cint(result(0))<br/>
        <br/>
        Exit Function<br/>
        <br/>
    

    processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
        Exit Function<br/>
    End Function<br/>
    <br/>
    <br/>
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
    '<br/>
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
    Function CheckUntil(Formulas As Variant, flag As Boolean) As Boolean<br/>
        On Error Goto processError<br/>
        <br/>
        Dim lastFormula As String<br/>
        Dim result As Variant<br/>
        Forall f In Formulas<br/>
            lastFormula = f.string1<br/>
            result = TestFormula(f.string1)<br/>
            If result = flag  Then<br/>
                CheckUntil = True<br/>
                Exit Function<br/>
            End If<br/>
        End Forall<br/>
        <br/>
        Exit Function<br/>
        <br/>
    

    processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
        Exit Function<br/>
    End Function<br/>
    <br/>
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
    '<br/>
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
    Function Check(Formulas As Variant, flag As Boolean) As Boolean<br/>
        On Error Goto processError<br/>
        <br/>
        Dim lastFormula As String<br/>
        Dim result As Variant<br/>
        Forall f In Formulas<br/>
            lastFormula = f.string1<br/>
            result = TestFormula(f.string1)<br/>
            If result = flag  Then<br/>
                Check = True<br/>
            End If<br/>
        End Forall<br/>
        <br/>
        Exit Function<br/>
        <br/>
    

    processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
        Exit Function<br/>
    End Function<br/>
    
    • FieldEngine Subclass with HideWhen Formulas....

      By Eric H Romo 2 decades ago

      I took the FieldValidator class and made a subclass, FieldEngine which adds a HiddenFormula list and functions similar to the validation functions to get some HideWhen functionality.



      I also modified the settings form to have a HideWhenFormula field that is meant to be like the FieldFormulas field.



      '=======================================================

      '** FieldEngine Class

      '=======================================================

      Class FieldEngine As FieldValidator

      Private HiddenFormula List As StringPair<br/>
      Private hfcount As Long<br/>
      <br/>
      Private Sub lsdoc_description<br/>
      

      %REM

      %END REM

      End Sub<br/>
      <br/>
      '/**<br/>
      ' * Create a new FieldEngine object, referencing the given fieldName<br/>
      ' * and using the given default error message if validation fails.<br/>
      ' */<br/>
      Public Sub New (fieldName As String, message As String)<br/>
          Me.FieldName = fieldName<br/>
          Me.InvalidMessage = message<br/>
          isString = True<br/>
          AllFormulas = True<br/>
          AllPatterns = True<br/>
          Set langStrings = New EnglishValidatorLanguageStrings()<br/>
      End Sub<br/>
      <br/>
      Public Function AddHiddenFormula (newFormula As String, errorMessage As String) As FieldValidator<br/>
          If (Len(Trim(newFormula)) &gt; 0) Then<br/>
              hfcount = hfcount + 1<br/>
              Dim sp As New StringPair(newFormula, errorMessage)<br/>
              Set HiddenFormula(fcount) = sp<br/>
          End If<br/>
          Set AddHiddenFormula = Me<br/>
      End Function<br/>
      <br/>
      '/**<br/>
      ' * Run all validation formulas against the field value<br/>
      ' */<br/>
      Private Function RunFormulaValidation () As Integer<br/>
          On Error Goto processError<br/>
          <br/>
          If (fcount = 0) Then<br/>
              RunFormulaValidation = True<br/>
              Exit Function<br/>
          End If<br/>
          <br/>
          Dim lastFormula As String<br/>
          Dim replaceString As String<br/>
          Dim result As Variant<br/>
          <br/>
          If Me.IsString Then<br/>
              replaceString = |&quot;| &amp; fieldValue &amp; |&quot;|<br/>
          Elseif Me.IsNumber Then<br/>
              replaceString = fieldValue<br/>
          Else<br/>
              replaceString = |[| &amp; fieldValue &amp; |]|<br/>
          End If<br/>
          <br/>
          Dim f As String<br/>
          Forall vf In ValidFormula<br/>
              lastFormula = vf.string1<br/>
              result = TestFormula(lastFormula)<br/>
              result = Cint(result(0))<br/>
              <br/>
              If (result = 1) And Not AllFormulas Then<br/>
                  ' * only a single success required<br/>
                  RunFormulaValidation = True<br/>
                  Exit Function<br/>
              Elseif (result = 0) And AllFormulas Then<br/>
                  ' * a single failure means the whole thing failed<br/>
                  returnString = vf.string2<br/>
                  failureString = langStrings.FORMULA_VALIDATION_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
                  Exit Function<br/>
              End If<br/>
          End Forall<br/>
          <br/>
          If AllFormulas Then<br/>
              ' * all formulas required, and none of them failed<br/>
              RunFormulaValidation = True<br/>
          Else<br/>
              ' * only one success required, and nothing worked<br/>
              failureString = langStrings.FORMULA_NO_MATCHES_ERROR<br/>
          End If<br/>
          Exit Function<br/>
          <br/>
      

      processError:

          failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
          Exit Function<br/>
      End Function<br/>
      <br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
      '<br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
      Function Hidden As Boolean<br/>
          On Error Goto processError<br/>
      


          Hidden = Check(HiddenFormula, True)<br/>
          <br/>
          Exit Function<br/>
          <br/>
      

      processError:

          failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
          Exit Function<br/>
      End Function<br/>
      <br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
      '<br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
      Function TestFormula(Formula As String) As Boolean<br/>
          On Error Goto processError<br/>
          <br/>
          Dim lastFormula As String<br/>
          Dim replaceString As String<br/>
          Dim result As Variant<br/>
          Dim t As String<br/>
          <br/>
          If Me.IsString Then<br/>
              replaceString = |&quot;| &amp; fieldValue &amp; |&quot;|<br/>
          Elseif Me.IsNumber Then<br/>
              replaceString = fieldValue<br/>
          Else<br/>
              replaceString = |[| &amp; fieldValue &amp; |]|<br/>
          End If<br/>
          <br/>
          lastFormula = Formula<br/>
          t = Replace(Formula, |@ThisValue|, replaceString)<br/>
          result = Evaluate(t, doc)<br/>
          TestFormula = Cint(result(0))<br/>
          <br/>
          Exit Function<br/>
          <br/>
      

      processError:

          failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
          Exit Function<br/>
      End Function<br/>
      <br/>
      <br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
      '<br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
      Function CheckUntil(Formulas As Variant, flag As Boolean) As Boolean<br/>
          On Error Goto processError<br/>
          <br/>
          Dim lastFormula As String<br/>
          Dim replaceString As String<br/>
          Dim result As Variant<br/>
          Forall f In Formulas<br/>
              lastFormula = f.string1<br/>
              result = TestFormula(f.string1)<br/>
              If result = flag  Then<br/>
                  CheckUntil = True<br/>
                  Exit Function<br/>
              End If<br/>
          End Forall<br/>
          <br/>
          Exit Function<br/>
          <br/>
      

      processError:

          failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
          Exit Function<br/>
      End Function<br/>
      <br/>
          ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''<br/>
      '<br/>
      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''    <br/>
      Function Check(Formulas As Variant, flag As Boolean) As Boolean<br/>
          On Error Goto processError<br/>
          <br/>
          Dim lastFormula As String<br/>
          Dim replaceString As String<br/>
          Dim result As Variant<br/>
          Forall f In Formulas<br/>
              lastFormula = f.string1<br/>
              result = TestFormula(f.string1)<br/>
              If result = flag  Then<br/>
                  Check = True<br/>
              End If<br/>
          End Forall<br/>
          <br/>
          Exit Function<br/>
          <br/>
      

      processError:

          failureString = langStrings.FORMULA_RUNTIME_ERROR &amp; &quot;: &quot; &amp; lastFormula<br/>
          Exit Function<br/>
      End Function<br/>
      <br/>
      <br/>
      

      End Class

      • Revised class

        By Eric H Romo 2 decades ago

        I removed the CheckUntil method (unnecessary)



        The Check method was renamed to CheckFormulas



        '=======================================================

        ' FieldEngine Class

        '=======================================================

        Class FieldEngine As FieldValidator

        Private HiddenFormula List As StringPair

        Private hfcount As Long



        Private Sub lsdoc_description

        %REM

        %END REM

        End Sub



        '/


        ' * Create a new FieldEngine object, referencing the given fieldName

        ' * and using the given default error message if validation fails.

        ' /

        Public Sub New (fieldName As String, message As String)

        Me.FieldName = fieldName

        Me.InvalidMessage = message

        isString = True

        AllFormulas = True

        AllPatterns = True

        Set langStrings = New EnglishValidatorLanguageStrings()

        End Sub



        Public Function AddHiddenFormula (newFormula As String, message As String) As FieldValidator

        If (Len(Trim(newFormula)) > 0) Then

        hfcount = hfcount + 1

        Dim sp As New StringPair(newFormula, message)

        Set HiddenFormula(fcount) = sp

        End If

        Set AddHiddenFormula = Me

        End Function



        '/


        ' * Run all validation formulas against the field value

        '
        /

        Private Function RunFormulaValidation () As Integer

        On Error Goto processError



        If (fcount = 0) Then

        RunFormulaValidation = True

        Exit Function

        End If



        Dim lastFormula As String

        Dim replaceString As String

        Dim result As Variant



        If Me.IsString Then

        replaceString = |"| & fieldValue & |"|

        Elseif Me.IsNumber Then

        replaceString = fieldValue

        Else

        replaceString = |[| & fieldValue & |]|

        End If



        Dim f As String

        Forall vf In ValidFormula

        lastFormula = vf.string1

        result = TestFormula(lastFormula)

        result = Cint(result(0))



        If (result = 1) And Not AllFormulas Then

        ' * only a single success required

        RunFormulaValidation = True

        Exit Function

        Elseif (result = 0) And AllFormulas Then

        ' * a single failure means the whole thing failed

        returnString = vf.string2

        failureString = langStrings.FORMULA_VALIDATION_ERROR & ": " & lastFormula

        Exit Function

        End If

        End Forall



        If AllFormulas Then

        ' * all formulas required, and none of them failed

        RunFormulaValidation = True

        Else

        ' * only one success required, and nothing worked

        failureString = langStrings.FORMULA_NO_MATCHES_ERROR

        End If

        Exit Function



        processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR & ": " & lastFormula

        Exit Function

        End Function



        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        '

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Function Hidden As Boolean

        On Error Goto processError



        Hidden = CheckFormulas(HiddenFormula, True)



        Exit Function



        processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR & ": " & lastFormula

        Exit Function

        End Function



        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        '

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Function TestFormula(Formula As String) As Boolean

        On Error Goto processError



        Dim lastFormula As String

        Dim replaceString As String

        Dim result As Variant

        Dim t As String



        If Me.IsString Then

        replaceString = |"| & fieldValue & |"|

        Elseif Me.IsNumber Then

        replaceString = fieldValue

        Else

        replaceString = |[| & fieldValue & |]|

        End If



        lastFormula = Formula

        t = Replace(Formula, |@ThisValue|, replaceString)

        result = Evaluate(t, doc)

        TestFormula = Cint(result(0))



        Exit Function



        processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR & ": " & lastFormula

        Exit Function

        End Function





        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        '

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Function CheckFormulas(Formulas As Variant, flag As Boolean) As Boolean

        On Error Goto processError



        Dim lastFormula As String

        Dim replaceString As String

        Dim result As Variant

        Forall f In Formulas

        lastFormula = f.string1

        result = TestFormula(f.string1)

        If result = flag Then

        CheckFormulas = True

        Exit Function

        End If

        End Forall



        Exit Function



        processError:

        failureString = langStrings.FORMULA_RUNTIME_ERROR & ": " & lastFormula

        Exit Function

        End Function





        End Class