By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

Static Variable problem

P: 35
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
Jan 20 '08 #1
Share this Question
Share on Google+
7 Replies


lotus18
100+
P: 866
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
Jan 21 '08 #2

P: 35
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.


Expand|Select|Wrap|Line Numbers
  1.  
  2. Static DefaultLoc As String
  3. Static DefaultCust As String
  4.  
  5.     If DefaultCust = "" Or DefaultLoc = "" Then 'Check if customer or location if is null
  6.                 DefaultLoc = "SDC"
  7.                 DefaultCust = "Union Pacific Railroad"
  8.  
  9.     End If
  10.  
  11.  
Jan 21 '08 #3

lotus18
100+
P: 866
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim DefaultLoc As String
  3. Dim DefaultCust As String
  4.  
  5.     If DefaultCust = "" Or DefaultLoc = "" Then 'Check if customer or location if is null
  6.                 DefaultLoc = "SDC"
  7.                 DefaultCust = "Union Pacific Railroad"
  8.  
  9.     End If
  10.  
  11.  
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
Jan 21 '08 #4

P: 35
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?
Jan 21 '08 #5

Expert 5K+
P: 8,434
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?
Jan 21 '08 #6

P: 35
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.
Jan 21 '08 #7

Expert 5K+
P: 8,434
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.
Jan 21 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.