Retaining Form Values After Unloading

D

dubbglubb

Hey Folks,

My Word Userform has several textboxes, lables and and a couple o
toggles.

When you "Okay" the form my associated DocVariables are updated in th
document.

Whilst loaded in memory the form retains all the entered values so i
the user opens the form again they can see everything previousl
entered.

If the user then closes the document, or unloads the form, the value
will naturally clear.

I'd like some way to retain the values so that if the user re-opens th
document form they can see, in each field, everything that wa
previously entered.

Can this be done within the document (preferable) or do you have t
export the vaules to a separate .txt file and import each time (o
something like that)?


Cheer
 
S

Simon Lloyd

Declare some public variables in a standard module like this:
Code
-------------------
Public s As String, t As String, u As Strin
-------------------
then assign those variables to your boxes before the form is unloade
then on intialize give those values to the boxes again
-------------------
dubbglubb;424562 Wrote:
Hey Folks

My Word Userform has several textboxes, lables and and a couple o
toggles

When you "Okay" the form my associated DocVariables are updated in th
document

Whilst loaded in memory the form retains all the entered values so i
the user opens the form again they can see everything previousl
entered

If the user then closes the document, or unloads the form, the value
will naturally clear

I'd like some way to retain the values so that if the user re-opens th
document form they can see, in each field, everything that wa
previously entered

Can this be done within the document (preferable) or do you have t
export the vaules to a separate .txt file and import each time (o
something like that)

Cheer

--
Simon Lloy

Regards,
Simon Lloyd
'The Code Cage' (http://www.thecodecage.com
 
C

Cindy M.

Hi Dubbglubb,
Can this be done within the document (preferable) or do you have to
export the vaules to a separate .txt file and import each time (or
something like that)?

Why not re-load the values from the document variables? Basically,
the reverse process of what your Form is doing when it's "OKed". You
can call this code from the UserForm_Initialize event.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
D

dubbglubb

Simon said:
Declare some public variables in a standard module like this: > Code:
--------------------


Thanks Simon,

So I coded:


Code:
--------------------


Public strOurRef As String, strClient As String, strContactTitle As String, _
strContactName As String, strAddress As String, strSuburb As String, _
strClientState As String, strPostCode As String, strPrelimContactName As String
Public boNataOnOff As Boolean



Private Sub UserForm_Initialize()


txtOurRef.Text = strOurRef
txtClient.Text = strClient
txtContactTitle.Text = strContactTitle
txtContactName.Text = strContactName

txtAddress.Text = strAddress
txtSuburb.Text = strSuburb
txtClientState.Text = strClientState
txtPostCode.Text = strPostCode
txtPrelimContactName.Text = strPrelimContactName

tgNataOnOff.Value = boNataOnOff


End Sub
--------------------


And ended with


Code:
--------------------
Private Sub UserForm_Terminate()

strOurRef = txtOurRef.Text
strClient = txtClient.Text
strContactTitle = txtContactTitle.Text
strContactName = txtContactName.Text
strAddress = txtAddress.Text
strSuburb = txtSuburb.Text
strClientState = txtClientState.Text
strPostCode = txtPostCode.Text
strPrelimContactName = txtPrelimContactName.Text
boNataOnOff = tgNataOnOff.Value

End Sub

--------------------



This did not however save the values. When I re-open the document and
form all my fields were blank. Any ideas?

Cheers
 
C

Cindy M.

Hi Dubbglubb,
This did not however save the values. When I re-open the document and
form all my fields were blank. Any ideas?

Please see my response to you. The values assigned to public variables
will be lost when the project is closed or reset, so the proposed
approach cannot work.

You will need to read the values BACK from the document variables in
UserForm initialize. Just use the "reverse" code for writing the
content TO the document variables...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
G

Gordon Bentley-Mix on news.microsoft.com

RE: Whilst loaded in memory the form retains all the entered values so if the
user opens the form again they can see everything previously entered.

Keeping the UserForm loaded is *not* a good idea - especially if there's a
chance that the user might have have two or more documents containing the
UserForm open at the same time and moreso if you are using the "magic forms"
functionality of VBA (wherein you just refer to the UserForm by its name;
e.g. "UserForm1.Hide", etc.). Keeping the UserForm loaded ties up system
resources, and you could have some major conflicts if there are multiple
UserForms loaded at the same time.

Better to unload the UserForm and then reinitialise it with the values from
the DocVariables when it's redisplayed, using the process Cindy has
recommended.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
K

Karl E. Peterson

Gordon said:
Keeping the UserForm loaded is *not* a good idea - especially if there's a
chance that the user might have have two or more documents containing the
UserForm open at the same time and moreso if you are using the "magic forms"
functionality of VBA (wherein you just refer to the UserForm by its name;
e.g. "UserForm1.Hide", etc.).

I agree that using the default instance is a *very* poor practice, and would
definitely advise always creating a new instance of forms to avoid the sort of
trouble you suggest may happen. But I'm intrigued by that suggestion, as it points
toward a way that multiple documents may be able to somewhat easily share data.
Would two documents based on the same template actually share the same default
instance of userforms?
Keeping the UserForm loaded ties up system resources,

Negligible, but true.
and you could have some major conflicts if there are multiple
UserForms loaded at the same time.
Huh?

Better to unload the UserForm and then reinitialise it with the values from
the DocVariables when it's redisplayed, using the process Cindy has
recommended.

Probably, unless you know WTH you're doing, yeah. :)
 
D

dubbglubb

Okay so I've recreated the form once again. VBA and the document just
weren't talking to each other.

Kind of annoying but turned out to be a good opportunity to revise,
clean up my code.

I used the process Cindy M. suggested (Cheers) , sourcing Variables
from the document. This was no mean feat given the complexity options
I'm giving the user.

Sourcing the form fields from my DocVariables meant that each
DocVariable had to have some sort of value otherwise the form wouldn't
load. I got around this by nesting if fields.

So, when updating to the document:


Code:
With ActiveDocument

..Variables("dvNata").Value = tgNataOnOff.Value
Then in the document

{ IF { DOCVARIABLE dvNata } = “True” “N” }

And finaly when reloading:


Code:
With ActiveDocument

If .Variables("dvNata").Value = "True" Then
lblNata.Caption = N
tgNataOnOff.Value = True
Else
lblNata.Caption = ""
tgNataOnOff.Value = False
End If
This works prefectly.

I also took onboard what you said Gordon Bentley, and given my users
will often have multiple documents open I've made my document only
load/unload, which I guess always makes ActiveDocument correct.
 
C

Cindy M.

Hi Karl,

You'd need to search back two or three years to some discussions
in this group in which Steve Hudson (the Word Heretic)
participated. He laid out pretty clearly what sort of conflicts
can arise if one takes advantage of the "freedoms" VBA allows
when showing forms without instantiating proper class instances.
I can't recall all the details, at this remove :)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17
2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 
K

Karl E. Peterson

Cindy said:
Hi Karl,


You'd need to search back two or three years to some discussions
in this group in which Steve Hudson (the Word Heretic)
participated. He laid out pretty clearly what sort of conflicts
can arise if one takes advantage of the "freedoms" VBA allows
when showing forms without instantiating proper class instances.
I can't recall all the details, at this remove :)

Yeah, okay, I'm very aware of the evils of using a default instance.

Wasn't sure that was what was being referred to. Thanks...
 

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