Overflow (Not a newbie question) VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tophurious
    New Member
    • May 2007
    • 13

    #1

    Overflow (Not a newbie question) VBA

    First, let me start by saying I know what an overflow is (Too large of a value into something that can't hold it, i.e. 35000 into an int, or 0/0, etc).

    Now to the issue. I have a class that I am trying to apply a value to one of its properties.

    Code:
    ...
    Streams.Item(rsName!Description).HInput = STMPTH(rs!P, rs!T)
    ...
    
    <in class>
    Property Let HInput(HInput As Double) 'Set the Enthalpy Input Value
        pHInput = HInput
        pH = pHInput
    End Property
    where STMPTH() is a Steam Table look up method that takes a Double P and Double T and returns a Double H (Pressure, Temperature, return Enthalpy)

    There are over 100 items in the Streams collection and only 2 throw an over flow error at the first line of code I have put up here. Now, before you ask, I do not have access to the code of the STMPTH() method (it's in a DLL file that my company did not create). However, I know the method does work. When the overflow error is thrown, the compiler stops on that line:

    Code:
    Streams.Item(rsName!Description).HInput = STMPTH(rs!P, rs!T)
    So, I decide to use Step Through (F8) to try to debug the issue (thinking maybe I'm passing a negative value or something). But when I hit F8, the program continues without a hitch.

    This is far from the first time I encountered this "phenomena" and have worked around it previously by catching the error and retrying the calculation (which worked. Example:

    Code:
    Property Get H() As Double 'Return Enthalpy of Stream
        H = 0
        On Error GoTo HErr
        If pSameAs = "" Then
            If pH <= 0 Then 'Enthalpy Not Known
                If pT > 0# And pP > 0# Then 'Calculate From Pressure and Temperature
                    pH = STMPTH(pP, pT)
                ElseIf pT > 0# Then
                    If pType = STREAMTYPE.Water Then
                        pH = STMTQH(pT, 0#)
                    End If
                ElseIf pP > 0# Then
                    If pType = STREAMTYPE.Water Then
                        pH = STMPQH(pP, 0#)
                    End If
                End If
            End If
        Else
            pH = Streams(pSameAs).H
        End If
        H = pH
        GoTo HExit
    HErr:
        H = 0
        pT = pT
        pP = pP
        If pSameAs = "" Then
            If pH <= 0 Then 'Enthalpy Not Known
                If pT > 0# And pP > 0# Then 'Calculate From Pressure and Temperature
                    pH = STMPTH(pP, pT)
                ElseIf pT > 0# Then
                    If pType = STREAMTYPE.Water Then
                        pH = STMTQH(pT, 0#)
                    Else
                        pH = STMTQH(pT, 1#)
                    End If
                ElseIf pP > 0# Then
                    If pType = STREAMTYPE.Water Then
                        pH = STMPQH(pP, 0#)
                    Else
                        pH = STMPQH(pP, 1#)
                    End If
                End If
            End If
        Else
            pH = Streams(SameAs).H
        End If
        H = pH
    HExit:
    End Property
    However, this little "work around" does not want to work in this case and I am now at a loss as to how to fix this.

    I have seen a couple of posts from other people on some forums with the same issue but they work around it by changing languages or compilers. I am limited to using VBA in access and am getting extremely frustrated with this problem.

    Does anyone know of a solution?

    Oh and I am using MS Access 2000 9.0.8961 SP-3
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, Tophurious.

    Actually I'm somewhat confused with the code of H get property.
    You check variable pSameAs and if it is empty string do calculations otherwise you call the same property in other instance of the class which may or may not call the same in another instance.
    Could you explain the logic?
    Could it be so that the property at certain conditions becomes recursive?

    Regards,
    Fish.

    Comment

    • Tophurious
      New Member
      • May 2007
      • 13

      #3
      The Get H Property is not where the issue is. That is an example of my work around. the problem is in the first part.

      As far as the sameas that simply checks to see if we had set that item's enthalpy to be equal to another item's. if it is use the other items. if not calculate.

      And if you notice the overflow error is not occurring on the Get H Property it is occuring in the Set HInput Property (which is included at the top)

      Other clarifications:

      Dims:
      pH: Double (private local)
      pHInput: Double (private local)
      rsName!Descript ion: String (holds the key of item in collection)
      rs!P: Double (pulled from database table, defined as double)
      rs!T: Double (pulled from database table, defined as double)

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        One more stupid question. Sorry.
        STMPQH() function is in the same DLL, isn't it?
        Did the code of H get property (before you've made the patch) throw the error when try to call particularly it?

        Comment

        • Tophurious
          New Member
          • May 2007
          • 13

          #5
          STMPQH() also in the DLL (Pressure, Quality, return Enthalpy). I do not remember if that was one that threw an error or not. The issue is not calling these methods.

          The previous issue was that the compiler would "forget" the value until you looked at it (that is the best way I can explain it). Basically, the run would throw an overflow and break. I would hover over the variable (say for example, pH) and the tooltip would say "pH = 0" even though I KNEW it wasn't so I would look at the local properties window and in there it would say pH = 1200 (or something). I go and hover over the variable again and it would say "pH = 1200" (This is still in break mode, so the variables shoule not have changed at all) Thats what I mean by "Forget".

          The current issue, is not that it is forgetting the value, it just throws an overflow. So I click run, when it hits that line (for only 2 out of the 100+ items in the collection) it throws an overflow and breaks. I click F8 to go forward and it works. Both rs!P and rs!T show proper values in break mode.

          To Summarize: Current Issue:
          Run == Overflow Error
          Step Through == A'OK!!!

          Comment

          • Stewart Ross
            Recognized Expert Moderator Specialist
            • Feb 2008
            • 2545

            #6
            Hi. You mention that the compiler stops at this line:
            Code:
            Streams.Item(rsName!Description).HInput = STMPTH(rs!P, rs!T)
            VBA passes values by reference by default (as pointers). You are passing recordset field references, which are not updatable, and should only be passed by value, not by reference. Unless the function headers define that these variables are indeed passed by value (which you won't know for STMPTH as it isn't yours) this can cause overflow and other errors if the internal function code tries to change the value (for example, by incrementing it or setting it to another value as a shortcut).

            I would suggest you assign the recordset fields to local variables and use these within the function calls instead.

            -Stewart

            Comment

            • Tophurious
              New Member
              • May 2007
              • 13

              #7
              Nice suggestion but nope.

              put this in:
              [CODE=vb]
              P = CDbl(rs!P)
              T = CDbl(rs!T)
              Streams.Item(rs Name!Descriptio n).HInput = STMPTH(P, T)
              [/CODE]

              same error, same spot. I had attempted this in my previous issue and it did not resolve it either. Still tried (hell I tried everything else I can think of)

              Just so you know, here is the thread I started back in june or july with my original issue (and no one could give me an answer then either)
              Here

              Comment

              • Stewart Ross
                Recognized Expert Moderator Specialist
                • Feb 2008
                • 2545

                #8
                Originally posted by Tophurious
                Nice suggestion but nope.

                put this in:
                Code:
                P = CDbl(rs!P)
                T = CDbl(rs!T)
                Streams.Item(rsName!Description).HInput = STMPTH(P, T)
                same error, same spot. I had attempted this in my previous issue and it did not resolve it either. Still tried (hell I tried everything else I can think of)
                OK. And you are certain that you are passing by value
                rsName!Descript ion in your streams call? Funnily enough it is with strings that I've had the overflow error when accidentally passing literals by reference.
                -Stewart
                -Stewart

                Comment

                • FishVal
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2656

                  #9
                  Actually I'm not so sure that variable values you see in debug mode are the same as at runtime.

                  I've made a little experiment.

                  Code module
                  [code=vb]
                  Public Sub Test()
                  Dim objClass1 As New Class1
                  objClass1.q = 1
                  End Sub
                  [/code]

                  Class module: Class1
                  [code=vb]
                  Private lngQ As Long

                  Public Property Get q() As Variant
                  lngQ = lngQ + 1
                  q = lngQ
                  End Property

                  Public Property Let q(ByVal vNewValue As Variant)
                  Err.Raise 9999
                  End Property
                  [/code]

                  You see Get property is never invoked in code and Let property simply throws a error, but the value of lngQ as well as q in locals window constantly increments when going into debug mode. That makes evidence that locals window invokes Get q() property to refresh itself.

                  I would suggest to get real runtime values of the variables. And maybe you'll need to code several attempts on the unstable code until you get a right value.

                  Regards,
                  Fish

                  Comment

                  Working...