473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to initialize a variable with the value used in last exit of the VBA application

subedimite
I have googled the functionality to use a Browse function to Look- in folders and then select required file from the folder.

However when I close the data base and open again, I would like VBA to remember the path name, so that I don’t have to Browse the folder and look for the same files again.

I suppose, some how I need to initialize a global variable with the value how it exited the database previously. Does this sound right?

What is the best way to do it?
May 17 '10 #1

✓ answered by Jim Doherty

@subedimite
There are various options open to you Global variables that are not constant is not one of them because they lose their scope once the database is closed or if you encounter an untrapped error during program flow.

The Easiest technique here, is this to create a one row table and call it UsystblConfigLocal (table names prefixed with 'Usys' automatically inherit the hidden atribute) that you can use to populate with all sorts of information and simply look your specific item up using the DLookup function at an appropriate point in your program ie: shutdown, when database first opens and so on.

Then there are more advanced methods ie database 'properties' that you can consider where you might create a property and append it to the properties collection at runtime.

Another simple little option you might want to consider is creating an 'external' file that sits in the same folder as your database something that the database can refer to on open or on close or at whatever other point in time you wish. This type of external file creation technique, is often used as a yardstick to measure against, if you understand me ie: if the file exists on the hard drive, do this, that or the other so to speak.

The following two separate functions will create and read the contents from an external file and return the value of that content to your calling function. Like I say.....there are many ways to do this... this is but one technique.

The PutFilePath function creates the dat file in the same folder as your database and relies on a full filepath and filename being passed to it (which of course is what you would be doing with the File dialog functionality when you 'click the button' so to speak.

The GetFileContents Function simply reads this small file (which gets replaced incidentally) each time you create and returns the content value to the calling procedure

Expand|Select|Wrap|Line Numbers
  1. Function PutFilePath(FilePath As String)
  2.     datpath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
  3.     Open datpath & "filepath.dat" For Output As #1
  4.         Print #1, FilePath & ",";
  5.     Close #1
  6. End Function
  7. Function GetFileContents() As String
  8.     Dim f As Integer
  9.     datpath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
  10.     datpath = datpath & "filepath.dat"
  11.     If Dir(datpath) <> "" Then
  12.         f = FreeFile
  13.         Open datpath For Binary Access Read As #f
  14.         mycontents = Input(LOF(f), f)
  15.         Close #f
  16.     End If
  17. End Function

Nothing I have said is intended to show you the best way, as that is always a subjective test, it is merely intended as a guider to certain options available to you. Ultimately you decide what best suits your purpose :)

Regards

6 2154
Jim Doherty
897 Expert 512MB
@subedimite
There are various options open to you Global variables that are not constant is not one of them because they lose their scope once the database is closed or if you encounter an untrapped error during program flow.

The Easiest technique here, is this to create a one row table and call it UsystblConfigLocal (table names prefixed with 'Usys' automatically inherit the hidden atribute) that you can use to populate with all sorts of information and simply look your specific item up using the DLookup function at an appropriate point in your program ie: shutdown, when database first opens and so on.

Then there are more advanced methods ie database 'properties' that you can consider where you might create a property and append it to the properties collection at runtime.

Another simple little option you might want to consider is creating an 'external' file that sits in the same folder as your database something that the database can refer to on open or on close or at whatever other point in time you wish. This type of external file creation technique, is often used as a yardstick to measure against, if you understand me ie: if the file exists on the hard drive, do this, that or the other so to speak.

The following two separate functions will create and read the contents from an external file and return the value of that content to your calling function. Like I say.....there are many ways to do this... this is but one technique.

The PutFilePath function creates the dat file in the same folder as your database and relies on a full filepath and filename being passed to it (which of course is what you would be doing with the File dialog functionality when you 'click the button' so to speak.

The GetFileContents Function simply reads this small file (which gets replaced incidentally) each time you create and returns the content value to the calling procedure

Expand|Select|Wrap|Line Numbers
  1. Function PutFilePath(FilePath As String)
  2.     datpath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
  3.     Open datpath & "filepath.dat" For Output As #1
  4.         Print #1, FilePath & ",";
  5.     Close #1
  6. End Function
  7. Function GetFileContents() As String
  8.     Dim f As Integer
  9.     datpath = Left(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
  10.     datpath = datpath & "filepath.dat"
  11.     If Dir(datpath) <> "" Then
  12.         f = FreeFile
  13.         Open datpath For Binary Access Read As #f
  14.         mycontents = Input(LOF(f), f)
  15.         Close #f
  16.     End If
  17. End Function

Nothing I have said is intended to show you the best way, as that is always a subjective test, it is merely intended as a guider to certain options available to you. Ultimately you decide what best suits your purpose :)

Regards
May 17 '10 #2
patjones
931 Expert 512MB
Hi,

I did something very similar to Jim, except with a text file. I don't know that there is necessarily any advantage to doing it my way, but here it is.

The write part looks like:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdWrite_Click()
  2.  
  3. Dim fs As FileSystemObject
  4. Dim ts As TextStream
  5. Dim strTextToWrite As String
  6.  
  7. strTextToWrite = Me.txtTextToWrite
  8.  
  9. Set fs = CreateObject("Scripting.FileSystemObject")
  10. Set a = fs.CreateTextFile("f:\data\Hold Variable.txt", True)
  11. ts.WriteLine (strTextToWrite)
  12. ts.Close
  13.  
  14. End Sub

I just wrote some text from a text box on a form to the file, but you would be assigning whatever you need to the string variable, and of course replacing my path with wherever you want to put the file.

To read it:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdReadText_Click()
  2.  
  3. Dim fs As FileSystemObject
  4. Dim ts As TextStream
  5. Dim strTextToRead As String
  6.  
  7. Set fs = CreateObject("Scripting.FileSystemObject")
  8. Set ts = fs.OpenTextFile("f:\data\Hold Variable.txt", ForReading)
  9. strTextToRead = ts.ReadLine
  10. ts.Close
  11.  
  12. Debug.Print strTextToRead
  13.  
  14. End Sub

The nice thing is, a text file with a short string in it takes up hardly any space. Hopefully one of our suggestions will work for you.

Pat
May 17 '10 #3
NeoPa
32,556 Expert Mod 16PB
Store the value in a table in your database.

** Edit **
As these two new posts have appeared since I first loaded the page I'll elaborate.

From what you say, I see no reason for anything more complicated than this. It was Jim's first suggestion. Other suggestions are always useful to see of course, and can prove useful in various situations. From what you tell us of yours though, this would appear to be the simplest, both conceptually and implementationally, and is fully adequate to the task.

Ultimately of course, what you decide is your own choice.
May 17 '10 #4
Thanks All.

Great
Hmmnnn... Let me read all this out and find out the best way. I do appriciate your Help.

Cheers
May 18 '10 #5
For What I am doing. I found it easier to create table( may be call it tblConfigurations, that will be written at exit of the data base and then read out next time when opening) as Jim suggested.



I need to take the data base in differnt Machines and if I created a new text file, I need to remember more set up files I need for the migration of the database.

Creating text files is also helpful for me for other applications as I am a Newbie, any new code is something valuable:)

Cheers All
May 27 '10 #6
NeoPa
32,556 Expert Mod 16PB
That describes clearly why the table option is probably the most apt for you, but as you say, other techniques are always good to add to your repertoire.
May 27 '10 #7

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

Similar topics

3
by: hendedav | last post by:
Hi gang. As with any other post, I am working on a project and have gotten stuck. I am trying to obtain a variable value in the parent webpage from an <iframe>. For instance: Parent Page code...
12
by: harishg2 | last post by:
Hi, How to store a variable value for more than one executions. Ex: main() { int i=0; i++; printf("%d",i);
1
by: Varadha | last post by:
Hi, I am declaring a variable static char Version_No = '1' in header file header.h In a application "app.exe", i am changing the value of the variable in to '2' In the same application i am...
3
by: Helpseeker | last post by:
Hi all, I have written a small code in which i declare a static int variable and increment its value by one each time i click on a button. actually i use the int variable value in a particular URL...
1
by: kong | last post by:
i'm very new to asp.net... i'm facing problem since i want to run the application... i put my connection string (strConnect) in a module (moduleConnect) then when my first form load, i want to...
2
by: duancg | last post by:
Hi, I wonder if someone could help since I wasn't able to find the answer through search. I have a simple .aspx page that shows data from a database table, as a table in UI. Now the data uses...
14
by: Neviton | last post by:
Help with this error ! Please ! The code below is simplified, because I'm trying to solve this error. ....MyClass.cpp In member function `void MyClass::Initialize()': ....MyClass.cpp aggregate...
0
by: tharika_c | last post by:
Hi, We have a simple ASP.NET web application where one of the Session variables, called Session("SSO_ID") gets created and assigned a value (equal to the HTTP_HRID request variable value),...
1
by: pendem | last post by:
I mean can set a variable value in a script to be unchanged even after page reload? for example if i set a value of a global variable "val" to 2 ; so using onbeforeunload() or onunload(), i will...
2
by: Jim in Arizona | last post by:
I'm making an app that inputs into multiple tables. I'm using the SCOPE_IDENTITY() SQL function for foreign key purposes. I input into the main table first and retrieve the scope_identity() and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.