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.