Change a constant? Keep a variable?

G

Guest

I have a value (string) that I want to maintain after the database is closed
and reopened. I've tried using a public variable but it appears that the
value disappears when the database is closed. Is there a way to keep it?
Alternatively, I can use a public constant but then the user can't change it.
Is there a way to either maintain the value of the public variable, or allow
a public constant to be changed (using code) by the user?
Thanks
Angela
 
D

Dirk Goldgar

Angela said:
I have a value (string) that I want to maintain after the database is
closed and reopened. I've tried using a public variable but it
appears that the value disappears when the database is closed. Is
there a way to keep it? Alternatively, I can use a public constant
but then the user can't change it. Is there a way to either maintain
the value of the public variable, or allow a public constant to be
changed (using code) by the user?

This is a database, after all. Why not store the value in a table you
maintain for that purpose? Use a function or a Property Let procedure
to change the value, and have that procedure update the value in the
table. Similarly, use a function or a Property Get procedure to
retrieve the value, and have that procedure read the value from the
table if the value isn't currently in memory.
 
J

Jesper Fjølner

I have a value (string) that I want to maintain after the database is
This is a database, after all. Why not store the value in a table you
maintain for that purpose? Use a function or a Property Let procedure
to change the value, and have that procedure update the value in the
table. Similarly, use a function or a Property Get procedure to
retrieve the value, and have that procedure read the value from the
table if the value isn't currently in memory.

Dirk, could you give me a small example of how to use Propyerty Get/Let in
this scenario (to retrieve or set af value)?
I'm using functions all the time, but want to get into using classes more,
but I just can't get my mind around in what scenarios to use them properly.
Thanks for any input.

Jesper, Denmark
 
T

Tim Ferguson

Dirk, could you give me a small example of how to use Propyerty
Get/Let in this scenario (to retrieve or set af value)?


I'm not Dirk, but here is a bit of code I had lying around...


Public Function GetDBProperty(PropertyName As String) As String
' Safe way to get a DB property
' If it doesn't exist, create it and return a ""

Dim db As Database
Dim con As Container
Dim doc As Document
Dim prp As Property

On Error Resume Next

Set db = CurrentDb() ' not DBEngine.Workspaces(0).Databases(0)

Set con = db.Containers("Databases")
If Err.Number > 0 Then
MsgBox Err.Number & ": " & Err.Description, vbCritical, _
"Databases Container"
GetDBProperty = ""
Exit Function

End If

Set doc = con.Documents("UserDefined")
If Err.Number > 0 Then
MsgBox Err.Number & ": " & Err.Description, vbCritical, _
"UserDefined Document"
GetDBProperty = ""
Exit Function

End If

Set prp = doc.Properties(PropertyName)
If Err.Number > 0 Then
Set prp = doc.CreateProperty(PropertyName, dbText, "")
doc.Properties.Append prp
doc.Properties.Refresh

End If
GetDBProperty = prp.Value

End Function

If you know that the property is there, you can do:

MyValue=CurrentDB().Containers("Databases").Documents( _
"UserDefined").Properties("MyProperty").Value


and the other way round is:


CurrentDB().Containers("Databases").Documents( _
"UserDefined").Properties("MyProperty").Value = MyValue


But with either of these you need to be able to catch the "Property Does
Nox Exist" error.

Hope that helps


Tim F
 
D

Dirk Goldgar

Jesper Fjølner said:
Dirk, could you give me a small example of how to use Propyerty
Get/Let in this scenario (to retrieve or set af value)?
I'm using functions all the time, but want to get into using classes
more, but I just can't get my mind around in what scenarios to use
them properly. Thanks for any input.

Hi, Jesper -- sorry I didn't get back to you right away. To answer your
question, see my response in another thread in this newsgroup:

Subject: Re: initializing global variables when they are defined?
Date: Sun, 16 Oct 2005 00:35:32 -0400
Message-ID: <[email protected]>
Newsgroups: microsoft.public.access.modulesdaovba
 
J

Jesper Fjølner

Dirk, could you give me a small example of how to use Propyerty
Hi, Jesper -- sorry I didn't get back to you right away. To answer your
question, see my response in another thread in this newsgroup:

Very interesting and new to me. Thanks very much!

Jesper Fjølner, Denmark
 

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