Static Variable problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Soulspike
    New Member
    • Jan 2008
    • 35

    Static Variable problem

    Hello,

    I am new to this forum and VBA so please bear with me. I have a fairly specific question I think but my terminoligy may be a little messed up.


    I want to declare a variable to remain even if access is closed and then re-opened. I thought i could do this by declareing the variable as static, but that doesnt seem to be working. Is there another way?

    Thank you for your time and patience
  • lotus18
    Contributor
    • Nov 2007
    • 865

    #2
    Originally posted by Soulspike
    Hello,

    I am new to this forum and VBA so please bear with me. I have a fairly specific question I think but my terminoligy may be a little messed up.


    I want to declare a variable to remain even if access is closed and then re-opened. I thought i could do this by declareing the variable as static, but that doesnt seem to be working. Is there another way?

    Thank you for your time and patience
    Can you post your code?

    Rey Sean

    Comment

    • Soulspike
      New Member
      • Jan 2008
      • 35

      #3
      Here it is.


      I want to run the following code at start up and force a default if the variables are blank. If they are not blank I leave them alone and use the variables to set the value of some form controls later.


      Code:
       
      Static DefaultLoc As String
      Static DefaultCust As String
      	
      	If DefaultCust = "" Or DefaultLoc = "" Then 'Check if customer or location if is null
      				DefaultLoc = "SDC"
      				DefaultCust = "Union Pacific Railroad"
      
      	End If

      Comment

      • lotus18
        Contributor
        • Nov 2007
        • 865

        #4
        [Code=vb]

        Dim DefaultLoc As String
        Dim DefaultCust As String

        If DefaultCust = "" Or DefaultLoc = "" Then 'Check if customer or location if is null
        DefaultLoc = "SDC"
        DefaultCust = "Union Pacific Railroad"

        End If

        [/code]

        Hey

        Why don't you try to change your variable declaration to Dim instead of Static? I'm not sure about this, Static is just like a Const (constant).

        Rey Sean
        Last edited by lotus18; Jan 21 '08, 04:16 AM. Reason: .

        Comment

        • Soulspike
          New Member
          • Jan 2008
          • 35

          #5
          Well the problem is that I need the variable to hold its value when I close the application and then open it again. Currently the values go away when I close access. How can I have vb save the values somewhere that I can then retrieve them the next time I open the database?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            The situation is pretty much the same with most programming languages. Variables are only held in the computer's working memory. Once you exit a program, they're discarded. Generally speaking it doesn't matter what type they are.

            If you want them to persist between sessions then you need to store the information somewhere and retrieve it next time your program starts. Typical places to store data between sessions would be...
            • Windows registry
            • A text file
            • A database

            There are probably more exotic options (for instance, create a DLL which holds information for you in its address space and returns it when you call it later). But these are the main ones you'll probably want to consider.

            Given that you're working in Access, a database table might be the simplest, since you already know the database is there. So why not create a new table, dump your information into it before exiting, and read it back on entry?

            Comment

            • Soulspike
              New Member
              • Jan 2008
              • 35

              #7
              Well I was hopeing that I was missing something I am now storing the data in a table and it seems to be working well. Thank you very much for your expertise.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by Soulspike
                Well I was hopeing that I was missing something I am now storing the data in a table and it seems to be working well. Thank you very much for your expertise.
                No problem. Glad to see you've got it working.

                Comment

                Working...