Global variable losing value for no apparent reason

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sgmaxmp11

    #16
    Re: Global variable losing value for no apparent reason



    "Bob Quintal" <rquintal@sPAmp atico.caà¢Õ¹໠繢éͤÇÒÁ
    news:Xns9AE961F DB3E8CBQuintal@ 66.175.223.2...


    Comment

    • lyle fairfield

      #17
      Re: Global variable losing value for no apparent reason

      Are you saying that Resume Next results in pointers to Global
      Variables being released, ie a Global String Variable length is now
      zero?

      I've never experienced that.

      Here is some code that indicates it is not so.

      Global SomeGlobal$
      ' or Public SomeGlobal

      Public Sub SomeTest()

      SomeGlobal = "SomeString "
      Debug.Print "Before Error SomeGlobal = " & SomeGlobal

      On Error Resume Next
      Err = 0
      Debug.Print 3 / 0
      If Err.Number 0 Then Debug.Print _
      Err.Description & vbNewLine & "There was an error!"
      On Error GoTo 0

      Debug.Print "After Error SomeGlobal = " & SomeGlobal

      End Sub

      'Before Error SomeGlobal = SomeString
      'Division by zero
      'There was an error!
      'After Error SomeGlobal = SomeString

      I seldom use seldom Global Variables, preferring Modularly Scoped
      Variables which are referenced by Public Functions, Registry Settings
      and CurrentProject Properties. The Public Functions can meet the
      "value lost" case by beginning with
      If len(SomeGlobal) = 0 Then InitializeSomGl obal
      where InitializeSomeG lobal is a procedure that does exactly that.

      On Jul 28, 9:44 am, Bob Quintal <rquin...@sPAmp atico.cawrote:
      Good morning Teddy,
      >
      essentially, the why of it happening is that somewhere in the code
      there is an error that is incorrectly handled, perhaps just glossed
      over with a resume next, that causes Access to clear all the global
      variables. perhaps the issue is with a query that cannot be deleted,
      or something of that nature.
      >
      The second time the routine is run, the error doesn't occur, so your
      variable is present.
      >
      Q
      >
      teddysn...@hotm ail.com wrote in
      news:80d1339e-791a-44e6-8653-746bd409fca0
      @k13g2000hse.go oglegroups.co
      m:
      >
      >
      >
      >
      >
      Weird.
      >
      I have taken over responsibility for a legacy application, Access
      2k3, split FE/BE.
      >
      The client has reported a problem and I'm investigating.  I didn't
      write the application.
      >
      The AutoExec macro calls a function "InitApplicatio n" which, in
      turn, calls a function to set the value of a global string
      variable "MyStrVar" (yes, I know, globals....)
      >
      The main form has a "Quit" button, which examines the value of
      this global variable and performs an operation depending on the
      value. However, a breakpoint at this point in the code shows that
      the variable is empty (hence the problem).
      >
      After a good deal of trial and error, I have found out what is
      happening, but not why!  The following step-by-step guide shows
      what I've found (I've added either MsgBox or breakpoints to allow
      me to figure out what's going on).
      >
      1.  Autoexec for the first time - "MyStrVar" is emptly
      2.  Call function to load variable - "MyStrVar" correctly loaded
      3.  Call function to quit application -  "MyStrVar" empty -
      PROBLEM 4.  Application quits.
      >
      Repeat the above but with slight variation
      >
      1.  Autoexec for the first time - "MyStrVar" is emptly
      2.  Call function to load variable - "MyStrVar" correctly loaded
      3.  Call function to quit application -  "MyStrVar" empty -
      PROBLEM 4.  Press RESET button in code window and close form by
      flipping to design mode - "MyStrVar" still empty
      5.  Run Autoexec for the second time - "MyStrVar" is empty
      6.  Call function to load variable - "MyStrVar" correctly loaded
      7.  Call function to quit application -  "MyStrVar" now correctly
      loaded - WHY?
      >
      At no point in the program is there an assignment to "MyStrVar"
      except in the function to load the value.  The operations that I'm
      performing manually are simply to run the AutoExec macro, and
      pressing the "Quit" button on the form. So my questions are:
      >
      a) Why is it "losing" the value between setting it and calling the
      function to quit?
      >
      b) Why does this behaviour change if I press RESET and run it
      again?
      >
      In case it might help here is the relevant code:
      >
      ' Function to load the variable
      Public Sub GetAllQueries()
      >
          Dim rcd As DAO.Recordset
      >
          Set rcd = CurrentDb.OpenR ecordset("SELEC T
          tblDatabaseQuer ies.QryID
      FROM tblDatabaseQuer ies;")
          If rcd.RecordCount 0 Then
              With rcd
                  .MoveFirst
                  Do
                      MyStrVar = MyStrVar & "*" & !QryID & "*"
                      .MoveNext
                      Loop While Not (rcd.EOF)
              End With
          End If
      >
      End Sub
      >
      'Function to quit
      Private Sub CloseDatabase_C lick()
      Dim Ref As Reference
      Dim bar As CommandBar
      Dim Qry As QueryDef
      >
      For Each Ref In References
          If Ref.Name = "Word" Then References.Remo ve Ref
          Next Ref
      For Each bar In Application.Com mandBars
          bar.Enabled = True
          Next bar
      Call subfrmManageDat abaseQueries_En ter
      >
      For Each Qry In CurrentDb.Query Defs
          If InStr(MyStrVar , "*" & Qry.Name & "*") 0 Then
              CurrentDb.Query Defs.Delete (Qry.Name)
          End If
      Next Qry
      DoCmd.DeleteObj ect acTable, "tblEditedQueri es"
      DoCmd.Quit
      End Sub
      >
      --
      Bob Quintal
      >
      PA is y I've altered my email address.
      ** Posted fromhttp://www.teranews.co m**

      Comment

      • Bob Quintal

        #18
        Re: Global variable losing value for no apparent reason

        lyle fairfield <lyle.fairfield @gmail.comwrote in
        news:a47f0afd-7f81-47a0-aa2d-4f5594c0f5ea@
        34g2000hsh.goog legroups.com
        :
        Are you saying that Resume Next results in pointers to Global
        Variables being released, ie a Global String Variable length is
        now zero?

        There exists and has to my recollection always existed a bug where
        global variables are reset on some or all unhandled errors. I do
        remember having a similar problem with an error that was handled by
        a resume next, many years ago, (Access '97, maybe even Access 2).
        Perhaps that was fixed.

        How I wrote code years ago is different than how I code today.
        That comes from experience gained on using the tools and features of
        the language. I seldom use globals any more and pay more attention
        to error handlers.

        >
        I've never experienced that.
        >
        Here is some code that indicates it is not so.
        >
        Global SomeGlobal$
        ' or Public SomeGlobal
        >
        Public Sub SomeTest()
        >
        SomeGlobal = "SomeString "
        Debug.Print "Before Error SomeGlobal = " & SomeGlobal
        >
        On Error Resume Next
        Err = 0
        Debug.Print 3 / 0
        If Err.Number 0 Then Debug.Print _
        Err.Description & vbNewLine & "There was an error!"
        On Error GoTo 0
        >
        Debug.Print "After Error SomeGlobal = " & SomeGlobal
        >
        End Sub
        >
        'Before Error SomeGlobal = SomeString
        'Division by zero
        'There was an error!
        'After Error SomeGlobal = SomeString
        >
        I seldom use seldom Global Variables, preferring Modularly Scoped
        Variables which are referenced by Public Functions, Registry
        Settings and CurrentProject Properties. The Public Functions can
        meet the "value lost" case by beginning with
        If len(SomeGlobal) = 0 Then InitializeSomGl obal
        where InitializeSomeG lobal is a procedure that does exactly that.
        >
        On Jul 28, 9:44 am, Bob Quintal <rquin...@sPAmp atico.cawrote:
        >Good morning Teddy,
        >>
        >essentially, the why of it happening is that somewhere in the
        >code there is an error that is incorrectly handled, perhaps just
        >glossed over with a resume next, that causes Access to clear all
        >the global variables. perhaps the issue is with a query that
        >cannot be deleted, or something of that nature.
        >>
        >The second time the routine is run, the error doesn't occur, so
        >your variable is present.
        >>
        >Q
        >>
        >teddysn...@hot mail.com wrote in
        >news:80d1339 e-791a-44e6-8653-746bd409fca0
        >@k13g2000hse.g ooglegroups.co
        >m:
        >>
        >>
        >>
        >>
        >>
        Weird.
        >>
        I have taken over responsibility for a legacy application,
        Access 2k3, split FE/BE.
        >>
        The client has reported a problem and I'm investigating.  I
        didn't write the application.
        >>
        The AutoExec macro calls a function "InitApplicatio n" which, in
        turn, calls a function to set the value of a global string
        variable "MyStrVar" (yes, I know, globals....)
        >>
        The main form has a "Quit" button, which examines the value of
        this global variable and performs an operation depending on the
        value. However, a breakpoint at this point in the code shows
        that the variable is empty (hence the problem).
        >>
        After a good deal of trial and error, I have found out what is
        happening, but not why!  The following step-by-step guide shows
        what I've found (I've added either MsgBox or breakpoints to
        allow me to figure out what's going on).
        >>
        1.  Autoexec for the first time - "MyStrVar" is emptly
        2.  Call function to load variable - "MyStrVar" correctly
        loaded 3.  Call function to quit application -  "MyStrVar"
        empty - PROBLEM 4.  Application quits.
        >>
        Repeat the above but with slight variation
        >>
        1.  Autoexec for the first time - "MyStrVar" is emptly
        2.  Call function to load variable - "MyStrVar" correctly
        loaded 3.  Call function to quit application -  "MyStrVar"
        empty - PROBLEM 4.  Press RESET button in code window and close
        form by flipping to design mode - "MyStrVar" still empty
        5.  Run Autoexec for the second time - "MyStrVar" is empty
        6.  Call function to load variable - "MyStrVar" correctly
        loaded 7.  Call function to quit application -  "MyStrVar" now
        correctly loaded - WHY?
        >>
        At no point in the program is there an assignment to "MyStrVar"
        except in the function to load the value.  The operations that
        I'm performing manually are simply to run the AutoExec macro,
        and pressing the "Quit" button on the form. So my questions
        are:
        >>
        a) Why is it "losing" the value between setting it and calling
        the function to quit?
        >>
        b) Why does this behaviour change if I press RESET and run it
        again?
        >>
        In case it might help here is the relevant code:
        >>
        ' Function to load the variable
        Public Sub GetAllQueries()
        >>
            Dim rcd As DAO.Recordset
        >>
            Set rcd = CurrentDb.OpenR ecordset("SELEC T
            tblDatabaseQuer ies.QryID
        FROM tblDatabaseQuer ies;")
            If rcd.RecordCount 0 Then
                With rcd
                    .MoveFirst
                    Do
                        MyStrVar = MyStrVar & "*" & !QryID &
        "*"
                        .MoveNext
                        Loop While Not (rcd.EOF)
                End With
            End If
        >>
        End Sub
        >>
        'Function to quit
        Private Sub CloseDatabase_C lick()
        Dim Ref As Reference
        Dim bar As CommandBar
        Dim Qry As QueryDef
        >>
        For Each Ref In References
            If Ref.Name = "Word" Then References.Remo ve Ref
            Next Ref
        For Each bar In Application.Com mandBars
            bar.Enabled = True
            Next bar
        Call subfrmManageDat abaseQueries_En ter
        >>
        For Each Qry In CurrentDb.Query Defs
            If InStr(MyStrVar , "*" & Qry.Name & "*") 0 Then
                CurrentDb.Query Defs.Delete (Qry.Name)
            End If
        Next Qry
        DoCmd.DeleteObj ect acTable, "tblEditedQueri es"
        DoCmd.Quit
        End Sub
        >>
        >--
        >Bob Quintal
        >>
        >PA is y I've altered my email address.
        >** Posted fromhttp://www.teranews.co m**
        >


        --
        Bob Quintal

        PA is y I've altered my email address.
        ** Posted from http://www.teranews.com **

        Comment

        • The Frog

          #19
          Re: Global variable losing value for no apparent reason

          I just create a class module, and instantiate it when the application
          starts. It has all the code necessary to handle the proper set up of
          global vars and I find it keeps things neat and clean. Never had a
          problem with it resetting to 'nothing'. If I want to know a value, I
          just check the associated property :-)

          Cheers

          The Frog

          Comment

          • Tony Toews [MVP]

            #20
            Re: Global variable losing value for no apparent reason

            teddysnips@hotm ail.com wrote:
            >I now know what the problem is, and it was a sort of error, but a
            >silent one. The application automates Word but for various reasons,
            >the reference to Word cannot be static and is set dynamically at start
            >up and removed during the closing routine:
            Good trouble shooting.

            However I would suggest an alternative approach to the Word reference issue.

            Late binding means you can safely remove the reference and only have an error when
            the app executes lines of code in question. Rather than erroring out while starting
            up the app and not allowing the users in the app at all. Or when hitting a mid, left
            or trim function call.

            This also is very useful when you don't know version of the external application
            will reside on the target system. Or if your organization is in the middle of moving
            from one version to another.

            For more information including additional text and some detailed links see the "Late
            Binding in Microsoft Access" page at http://www.granite.ab.ca/access/latebinding.htm

            As far as ADO vs DAO goes they both work just fine in Access and I see absolutely no
            reason to convert from one to another.

            And please ignore anything Aaron Kempf states. He is rather monomaniacial.

            Tony
            --
            Tony Toews, Microsoft Access MVP
            Please respond only in the newsgroups so that others can
            read the entire thread of messages.
            Microsoft Access Links, Hints, Tips & Accounting Systems at

            Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/

            Comment

            • rkc

              #21
              Re: Global variable losing value for no apparent reason

              On Jul 31, 7:49 am, Bob Quintal <rquin...@sPAmp atico.cawrote:
              lyle fairfield <lyle.fairfi... @gmail.comwrote in
              news:a47f0afd-7f81-47a0-aa2d-4f5594c0f5ea@
              34g2000hsh.goog legroups.com
              :
              >
              Are you saying that Resume Next results in pointers to Global
              Variables being released, ie a Global String Variable length is
              now zero?
              >
              There exists and has to my recollection always existed a bug where
              global variables are reset on some or all unhandled errors. I do
              remember having a similar problem with an error that was handled by
              a resume next, many years ago, (Access '97, maybe even Access 2).
              Perhaps that was fixed.
              >
              How I wrote code years ago is different than how I code today.
              That comes from experience gained on using the tools and features of
              the language. I seldom use globals any more and pay more attention
              to error handlers.
              On Error Resume Next handles errors as long as the code it is in is in
              scope
              and On Error Goto 0 has not been called.

              To extend Lyle's example with a call to another method:

              '------
              On Error Resume Next
              Err = 0
              Call CauseError
              If Err.Number 0 Then Debug.Print _
              Err.Description & vbNewLine & "There was an error!"
              On Error GoTo 0

              Debug.Print "After Error SomeGlobal = " & SomeGlobal
              '------

              Sub CauseError()
              Debug.Print 3 / 0
              End Sub

              The error in CauseError is handled by the calling code.

              I would be a very scary thing indeed if On Error Resume Next did not
              handle an error and global variables were reset because of it.




              Comment

              Working...