Global variable

G

Guest

Hello.
In my Access database, the user is asked to select a folder which path
defines a Global variable (c:\\whatever\). And everything is working fine.
However, when the user quit the application and reopen it again, he must
select the path all over again. Is this the only way?
Is possible that Access could storage the last Global variable defined in
previous sessions?
Thank you.
 
D

Douglas J. Steele

Four possibilities come to mind.

1) Create a property on the Database (using the DAO CreateProperty method)
and store the path there
2) Create a table to hold it
3) Write it to the registry (you can use SaveSetting and GetSetting for
this)
4) Use an INI file
 
G

Guest

Thank you, Douglas.
But can you explain a little more how can I do that (I am quite new in
Access, so not everything is obvious for me... sorry!)

I have in a customised menu the comand 'Specify Word documenst folder
location' with this code:

---
Option Explicit
Public strFolderName As String
Function Select_folder()
strFolderName = BrowseFolder("What Folder you want to select?")
End Function
---

Here the user specifiy the strFolderName variable. And I want it to be
stored forever, unless the user specify another one. Am I being clear?

Thank you for your effort.

Best regards.

Acores


"Douglas J. Steele" escreveu:
 
D

Douglas J. Steele

WARNING: The following is all untest air-code.

1) Create a property on the Database and store the path there

To create a Database property, use code like:

Sub SetDatabaseTextProperty( _
PropertyName As String, _
PropertyValue As String _
)
On Error GoTo SetDatabaseTextProperty

DIm dbCurr As DAO.Database
Dim prpCurr As DAO.Property

Set dbCurr = CurrentDb()
dbCurr.Properties(PropertyName) = PropertyValue

End_SetDatabaseTextProperty:
Set prpNew = Nothing
Set dbCurr = Nothing
Exit Sub

Err_SetDatabaseTextProperty:
Select Case Err.Number
Case 3270
' Error 3270 means that the property was not found.
' (i.e.: it doesn't exist yet)
Set prpNew = dbCurr.CreateProperty( _
PropertyName, dbText, PropertyValue)
dbCurr.Properties.Append prpNew
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description
Resume End_SetDatabaseTextProperty
End Select

End Sub

To retrieve that property, you'd use CurrentDb.Properties("NameOfProperty")
(Note that an error 3270 will be raised if the property doesn't exist)

In other words, rather than having strFolderName declared as a global
variable, you'd have:

Function Select_folder()
Dim strFolder As String

strFolder = BrowseFolder("What Folder you want to select?")
Call SetDatabaseTextProperty("FolderName", strFolder)

End Function

Everyplace in your code that you're currently using strFolderName, use
CurrentDb.Properties("FolderName") instead.

2) Create a table to hold it

Presumably you know how to create a table. Create one called, say,
DatabaseProperties with two columns: PropertyName and PropertyValue (both
text fields). Instead of the SetDatabaseTextProperty sub I showed above,
you'd have

Sub SetDatabaseTextProperty( _
PropertyName As String, _
PropertyValue As String _
)
On Error GoTo SetDatabaseTextProperty

Dim strSQL As String
Dim varProperty As Variant

varProperty = DLookup(_
"PropertyValue", _
"DatabaseProperties", _
"PropertyName ='" & PropertyName")

If IsNull(varProperty) Then
strSQL = "INSERT INTO DatabaseProperties " & _
"(PropertyName, PropertyValue) " & _
"VALUES (" & Chr$(34) & PropertyName & Chr$34 & ", " & _
Chr$(34) & PropertyValue & Chr$(34) & ")"
Else
strSQL = "UPDATE DatabaseProperties " & _
"SET PropertyValue = " & Chr$(34) & PropertyValue & Chr$(34) & _
" WHERE PropertyName = " & Chr$(34) & PropertyName & Chr$(34)
End If

CurrentDb.Execute strSQL, dbFailOnEreror

End_SetDatabaseTextProperty:
Exit Sub

Err_SetDatabaseTextProperty:
MsgBox Err.Number & ": " & Err.Description
Resume End_SetDatabaseTextProperty

End Sub

To retrieve that property, you'd use the DLookup statement in the sub above.
(Note that DLookup will return Null if the property doesn't exist)

In other words, rather than having strFolderName declared as a global
variable, you'd have:

Function Select_folder()
Dim strFolder As String

strFolder = BrowseFolder("What Folder you want to select?")
Call SetDatabaseTextProperty("FolderName", strFolder)

End Function


Everyplace in your code that you're currently using strFolderName, use
DLookup("PropertyValue", "DatabaseProperties", "PropertyValue =
'FolderName'") instead.

Post back if you want details on options 3 or 4.
 
G

Guest

Hi, Douglas.

A friend of mine told me about a simple procedure of creating a .txt file in
order to store the selected path.
Do you agree with him?
Thank you.


"Douglas J. Steele" escreveu:
 
D

Douglas J. Steele

That's another approach. It's not that different than using an INI file
(although it's not quite as flexible). I use INI files all the time.
 
G

Guest

But how should I use a .txt or INI file?
I tried to use the procedures you've suggested below but some how, they
didn't work (maybe I am doing something really wrong...).
Do you think that working with a .txt or INI file would be less complicated?
Thank you for all your effort.
Regards.
Acores

"Douglas J. Steele" escreveu:
 
D

Douglas J. Steele

Basically, all of the approaches I suggested are going to be about the same
amount of work (once you've got the routines written in the first place).

Realistically, I think your best approach is to store it in a table in your
application (option 2 from what I presented).

Saying "they didn't work" doesn't tell me anything. Unless you can give more
details, I won't be able to offer any suggestions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top