NEED TO SAVE DYNAMIC VALUES IN UNBOUND CONTROL OR FORM

G

Glint

Hi Guys,

I have two dynamic values, a date and a number, that I want to save in an
un-bound control in a form. I tried using the tag property of some text
boxes, but surprisingly, after setting the values the first time, the values
would not change thereafter. Here is what I did:

Forms!MyForm!MyControl.Tag=MyDate

This worked then. Now, it shows me my new value when I run the code. But
when I close the database, the old value is what I get next time I open it.
And I need the new values that next time.

I also tried using a label (caption property). I see the new values alright,
but next time I open the database, it is the old values that show up again.

How can I achieve my objective?
 
D

Douglas J. Steele

Why not store the value in a table, and then use DLookup to display them?
 
G

Glint

Thanks for your response Douglas.
I am trying to avoid storing the values in a table.
 
D

Douglas J. Steele

Why, if you require them later?

Values on forms are transient. The only way you can make values permanent is
to store them. You can store them in a table, you can store them as a
property of the database or you can store them in an external file (an INI
file or the registry), but you have to store them.
 
J

Justo Morales

The tag and label property are part of the form itself. Under normal
circumstances, these values are not saved and are discarded when the
form is closed. However, since you are changing the form itself, you
may be asked if you want to save upon closing the form. You may have
unknowingly saved the form the first time you ran the code. Yet, in
subsequent runs, you are not saving the form.

To accomplish what your objective, write code that saves the form. I
believe DoCmd.Save (or a derivative thereof) may work.

Please post whether or not this worked for you.

--)) Justo ((--
 
G

Glint

Justo,
Thanks for your suggestion. As a matter of fact, the code did include the
following save method:

DoCmd.Save acForm, "Open Sesame"

But it does not seem to work.
 
G

Glint

Thanks again, Douglas.

I am avoiding the tables because I found that some smart fellows open them
and read my values at the back end of the application. I am assuming that
placing the values inside the front end (which I intend to convert into an
MDE file) would make them more secure.
 
R

Rui

Just using Justo's suggestion, I would suggest you change your save method to

docmd.setwarnings false
docmd.Close acForm, "formname",acSaveYes
docmd.setwarnings true

Rui
 
J

Justo Morales

Glint,

You are right. I didn't check what I was writing about and wrote what
"in theory" should've worked. However, tag fields clear out when you
unload the form. I don't think there is a way to save the tag field in
the form.

However, I found out how the same can be accomplished by creating a new
property on the form itself. I tested the following code and it worked:

Private Sub Form_Open(Cancel As Integer)
Dim aob As AccessObject
Dim aop As AccessObjectProperty

Set aob = CurrentProject.AllForms(Me.Name)
For Each aop In aob.Properties
If aop.Name = "UnboundField1" Then
txtUnbound.Tag = aop.Value
Exit For
End If
Next aop

txtUnbound.Value = txtUnbound.Tag
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim aob As AccessObject

If Not Me.NewRecord Then
Set aob = CurrentProject.AllForms(Me.Name)
aob.Properties.Add "UnboundField1", Me.txtUnbound.Tag
End If
End Sub

Private Sub txtUnbound_AfterUpdate()
txtUnbound.Tag = txtUnbound.Value
End Sub

Of course, just change the field names to your liking.

Again, please post if this worked for you or not.
 
G

Glint

Rui,

Thanks, but that did not work either. Next time I opened the form, it was
still displaying the old values.
 
D

Douglas J. Steele

Not really. If your application can read the value, a determined user will
be able to as well.
 
G

Glint

Thank you very much Justo.
Sorry I did not get a notification of your post until now.
I am mightily interested in this approach. Can you please give a little
explanation of how the procedure works?
 
G

Glint

Wonderful, Justo.

It worked after a minor adjustment. I still like to know why it should not
be a new record in:

If Not Me.NewRecord Then
Set aob = CurrentProject.AllForms(Me.Name)
aob.Properties.Add "UnboundField1", Me.txtUnbound.Tag
End If

Then I suspected Me.txtUnbound.Tag should have been Me!txtUnbound.Tag. When
I commented out the if statement and made the minor ammendment, my unbound
textbox retained its value when the form opened next time.

Bravo, and thanks again.
 
L

Larry Linson

Glint said:
Thanks again, Douglas.
I guess I have no choice than to use a table then.

Why do you say that? Doug gave you three other choices... a property of the
database, a value in an INI file, and a value in an external file. Another
choice would be to create/store a value in the Windows Registry. Using a
table may well have some advantages, but smart guys can read that, too.

Larry
 

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