Subform displays deleted version of itself

S

Snaux

Here's a weird one:

I have a form that contains a dynamic subform - the subform is not
bound to any data, and instead contains a bunch of labels that I'm
creating dynamically using CreateControl.

Based on user interaction, I sometimes completely change this subform.
I set the SourceObject to empty (to unbind the subform from the Main
form), delete the subform, recreate the subform with new controls, and
rebind it to the main form:


formMain.subFormObject.SourceObject = ""
If FormExists("SubForm") Then
DoCmd.DeleteObject acForm, "SubForm"
End If
Set formSub = CreateForm
strSub = formSub.Name
'
' I add my custom controls to the form here
'
' Then I close and rename the subform, and rebind it:
DoCmd.Close acForm, strMultiBar, acSaveYes
DoCmd.Rename "SubForm", acForm, strSub
formMain.subFormObject.SourceObject = "SubForm"


90% of the time, this works just fine, and the subform is replaced with
new data correctly. Every so often, however, a real shocker occurs;
even though the Subform itself is deleted and recreated perfectly (you
can open it in design view to verify), the DISPLAY of the subform, when
it appears in the main form, it will show a previous (and deleted)
version of the subform.
From this point on, even if I physically drag a new copy of the subform
onto the main form, it still displays as its previous incarnation. But
if you open the sub by itself... it's fine!

I know what's going on - the database is looking at its garbage heap
for the form instead of looking at the live version. And indeed, doing
a "Compact And Repair" fixes the problem, but this seems a little
drastic (and it screws up my app in doing so).

Is there anything I can do to prevent and/or repair this odd behaviour
without actually repairing the entire database?
 
M

Marshall Barton

Snaux said:
Here's a weird one:

I have a form that contains a dynamic subform - the subform is not
bound to any data, and instead contains a bunch of labels that I'm
creating dynamically using CreateControl.

Based on user interaction, I sometimes completely change this subform.
I set the SourceObject to empty (to unbind the subform from the Main
form), delete the subform, recreate the subform with new controls, and
rebind it to the main form:


formMain.subFormObject.SourceObject = ""
If FormExists("SubForm") Then
DoCmd.DeleteObject acForm, "SubForm"
End If
Set formSub = CreateForm
strSub = formSub.Name
'
' I add my custom controls to the form here
'
' Then I close and rename the subform, and rebind it:
DoCmd.Close acForm, strMultiBar, acSaveYes
DoCmd.Rename "SubForm", acForm, strSub
formMain.subFormObject.SourceObject = "SubForm"


90% of the time, this works just fine, and the subform is replaced with
new data correctly. Every so often, however, a real shocker occurs;
even though the Subform itself is deleted and recreated perfectly (you
can open it in design view to verify), the DISPLAY of the subform, when
it appears in the main form, it will show a previous (and deleted)
version of the subform.

onto the main form, it still displays as its previous incarnation. But
if you open the sub by itself... it's fine!

I know what's going on - the database is looking at its garbage heap
for the form instead of looking at the live version. And indeed, doing
a "Compact And Repair" fixes the problem, but this seems a little
drastic (and it screws up my app in doing so).

Is there anything I can do to prevent and/or repair this odd behaviour
without actually repairing the entire database?


Actually, that's not all that unexpected. For several
reasons, using the Create... methods at runtime is an
extremely dangerous thing to do. This is especially true if
you do this sort of thing repeatedly. And don't count on
Compact/Repair fixing it every the time.

The Create... methods are there so you can use them in a
***design time*** wizard like procedure.

The standard alternative is to precreate the form with all
the controls you will need and then set their Visible, Top,
Left, Width, etc. properties as needed.
 

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