By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

form object persistence...including contents of controls

P: n/a
1) Module1 has the following delcaration:

Public g_frmZZZ as Form
Public g_txtForm2 as Variant

2) app has two forms: form1 and form2

3) a command button on form1 opens form2; it also has another command
button the purpose of which is to permit one to set a breakpoint in
the code behind the button

DoCmd.Open acForm, "form2"

4) form2 has a text box control: txtForm2

5) form2 has a command button which, when clicked, executes this code:

' save the form into a public variable
Set g_frmZZZ = Me
' save the contents of a control on the form into a public variable
g_txtForm2 = Me.txtForm2
' close this form
DoCmd.Close acForm, "form2", acSavePrompt

If I have a breakpoint set in the code behind the second button in
form1, and I do the following:
step 1: on form1 click command button to open form2
step 2: on form2, type in the letter "q" in the text box control
step 3: on form2, click the command button
step 4: on form 1, click the second command button...go to breakpoint

if I type the following in at the Debug window:
print g_frmZZZ.txtForm2 <press enter key>

I get run-time error 2467 with a message: "application-defined or
object-defined error"

if I type the following in at the Debug window:
print g_txtForm2 <press enter key>

it prints the letter "q" in the debug window.

If I comment out the DoCmd.Close line in the code behind the command
button in form2, and repeat the same steps, then if I type the
following in at the Debug window:
print g_frmZZZ.txtForm2 <press enter key>

it prints the letter "q" in the debug window.

I was thinking that by declaring g_frmZZZ as public and then setting
g_frmZZZ to form2 just before form2 is closed, it would have the
effect of taking a copy of form2 and putting it into g_frmZZZ. Based
on the tests I ran, it appears as though I was wrong about the whole
"make a copy of..." thing. In the second test (where I don't close
form2), I can interrogate the contents of a control on g_frmZZZ, but
if I close form2 (like I did in the first test) it doesn't know about
the txtForm2 control on g_frmZZZ.

Question 1:
So, if I'm interested in having the contents of the controls on a form
persist, are my two options:
a) keep the form open for as long as the app needs to interrogate the
contents of the controls
b) set up a public data structure where various elements in the data
structure correspond with controls on the form...then, if one needs to
close the form, run some code that populates that public data
structure with the contents of the control on the form

Question 2:
Is there a way to instantiate a new form object in such a way that:
a) the form gets created/populated (including contents of controls)
as a copy of an already
existing form
b) when the already-existing form gets closed, the just-instantiated
form is NOT affected, i.e. it
retains all the info it received (including contents of controls)
when instantiated

Thank you.

Dec 13 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Oko
In Relation to preserving form values:

Setting one variable equal to another can be done two ways: "By
Reference" or "By Value". "By Reference" means that you don't create a
copy - but rather point to where the value is in memory. This is not
unlike a library's catalog system telling you where a book is found.

Once the value is erased (or the book is checked out), your reference
to its location is invalid - and you find nothing instead. This seems
to be what's happening to you when your form closes.

"By Value" will actually create the copy of the value that you speak
of. A more stable response on the part of the VB interpreter - but I'm
not sure when it decides which strategy it will use.

What I've done in the past is leave the form open - but set
it's .visible property (it's not listed in the properties window,
gonna hafta trust me on this) to FALSE. This way the form disappears
as if closed and cannot be manipulated by the user - but the
information is still completely accessible.

In Relation to creating a copy of the form:

I am unsure what you are looking to achieve. If I knew this I may be
able to help you better.

mirandacasc...@yahoo.com wrote:
1) Module1 has the following delcaration:

Public g_frmZZZ as Form
Public g_txtForm2 as Variant

2) app has two forms: form1 and form2

3) a command button on form1 opens form2; it also has another command
button the purpose of which is to permit one to set a breakpoint in
the code behind the button

DoCmd.Open acForm, "form2"

4) form2 has a text box control: txtForm2

5) form2 has a command button which, when clicked, executes this code:

' save the form into a public variable
Set g_frmZZZ = Me
' save the contents of a control on the form into a public variable
g_txtForm2 = Me.txtForm2
' close this form
DoCmd.Close acForm, "form2", acSavePrompt

If I have a breakpoint set in the code behind the second button in
form1, and I do the following:
step 1: on form1 click command button to open form2
step 2: on form2, type in the letter "q" in the text box control
step 3: on form2, click the command button
step 4: on form 1, click the second command button...go to breakpoint

if I type the following in at the Debug window:
print g_frmZZZ.txtForm2 <press enter key>

I get run-time error 2467 with a message: "application-defined or
object-defined error"

if I type the following in at the Debug window:
print g_txtForm2 <press enter key>

it prints the letter "q" in the debug window.

If I comment out the DoCmd.Close line in the code behind the command
button in form2, and repeat the same steps, then if I type the
following in at the Debug window:
print g_frmZZZ.txtForm2 <press enter key>

it prints the letter "q" in the debug window.

I was thinking that by declaring g_frmZZZ as public and then setting
g_frmZZZ to form2 just before form2 is closed, it would have the
effect of taking a copy of form2 and putting it into g_frmZZZ. Based
on the tests I ran, it appears as though I was wrong about the whole
"make a copy of..." thing. In the second test (where I don't close
form2), I can interrogate the contents of a control on g_frmZZZ, but
if I close form2 (like I did in the first test) it doesn't know about
the txtForm2 control on g_frmZZZ.

Question 1:
So, if I'm interested in having the contents of the controls on a form
persist, are my two options:
a) keep the form open for as long as the app needs to interrogate the
contents of the controls
b) set up a public data structure where various elements in the data
structure correspond with controls on the form...then, if one needs to
close the form, run some code that populates that public data
structure with the contents of the control on the form

Question 2:
Is there a way to instantiate a new form object in such a way that:
a) the form gets created/populated (including contents of controls)
as a copy of an already
existing form
b) when the already-existing form gets closed, the just-instantiated
form is NOT affected, i.e. it
retains all the info it received (including contents of controls)
when instantiated

Thank you.
Dec 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.