Jon Furman wrote:
>This is a good one I think...should be ripe for comment and opinion. I wrote
>a class that generates a custom form with a variable number of controls and
>uses the module.inserttext method to create event handlers and background
>code for the form. To be more precise it opens the form in design view,
>creates the controls and the code then finally saves, closes the form and
>then reopens the form as a subform. This all works brilliantly and I thought
>that I was very clever.
>
>Then I ran into a problem....if I do this when I have another form open (one
>that has nothing to do the generated form or the form that contains it) and
>the unrelated form has it's own variables or static variables in it's
>functions then they are reinitialized (ie all numeric variables become 0)
>and the code in that form stops running correctly. The code in the class
>that's autogenerating the controls is fine and the container form and its
>code are fine also. Any other executing form code is toasted though. But
>wait....it gets worse. The next time I open the database and then open
>effected form, Access crashes. I cannot use the form that was coincidentally
>open when the autogenerate was run until I completely delete it and reimport
>it from a backup file. Very strange.
>
>Ideally I would like to find a way to keep the variable reinitialization
>from happening. I guess that I could write some code to prevent the
>autogenerate from running if other forms are open but stopping the problem
>from happening in the first place would be a lot better.
>
>I'm on Access 2002 and this is actually an ADP. Anyone else tried to be this
>fancy with VBA, gotten burnt like me and found a way around it?
Workarounds, I don't got, but, comments, I got plenty of
comments ;-)
Aside from the issue that you're doing this in an ADP, which
I have no experience with, this is a really, REALLY BAD
IDEA!
You've certainly run into one of the reasons this is a bad
idea. Having your project reset is very annoying.
In general, self creating/modifying code is right about the
bottom of the list of good proggramming practices.
Those CreateControl and module manipulating methods are only
provided for the folks that create DESIGN TIME wizards, they
are not intended to be used in a running application.
Saving a form and/or module is a heavy duty operation that
greately increases the odds of your project being corrupted
(it sounds like you've already seen this too). It also
causes major bloat that will force you to Compact the file
much more frequently than otherwise.
A far better way to get your form to do what you want is to
precreate all the controls you will need (with their event
procedures) and just make them visible and adjust their
other properties as needed. Besides, it's more
straightforward (and probably less) code than what you were
doing.
--
Marsh
MVP [MS Access]
|