Setting HasModule in code

  • Thread starter Thread starter sebt
  • Start date Start date
S

sebt

Watch out for this BUG in Access (2003)

I'm using a documentation tool called DocumentX (highly-recommended by
the way). It's a great product but doesn't pick up objects (forms,
reports) from your database unless they have a VBA module attached to
them.

So for documentation purposes I'm trying to give every form and report
a module. Loop through the Forms and Reports containers, and set
HasModule=True for those forms/reports that have this set to FAlse.

This doesn't work. If you then open up the form or report, its
HasModule property is set to True, but you can't access its module
(presumably because Access hasn't given it one).

You have to explicitly open each form/report in the UI, set HasModule
to True (or to False then back to True, if you previously set it using
VBA), cancelling the warning box when you set it to False, and then
open the VBA editor. Only then does Access actually create a module.

This is extremely annoying - I now have to do this for 35 forms and 68
reports. I wish that at least VBA would report "you cannot set the
HasModule property" - because, effectively, you can't set it! Trying
to set a type-Module variable to Form.Module after setting HasModule to
True gives a nasty error - because there is no Module.
 
I would consider that to be a bug in DocumentX. A good documenter shouldn't
insist that you do this. It should be able to document *all* objects.
 
sebt, I am not able to reproduce this issue in A2003 SP2.

This code did set the property correctly:

DoCmd.OpenForm "Form0", acDesign
Forms!Form0.HasModule = True
DoCmd.Close acForm, "Form0", acSaveYes

Note that you must be in design view, to do this kind of thing and have it
stick.
 
Hi Allen

Thanks for having a go at this. Your code is exactly the same as my
code.

The problem isn't with the HasModule property, but with the existence
of an associated module (i.e. something pointed at by the
ThisForm.Module property). The property does get set correctly in
code, as you found in your test. But setting the property to True
doesn't create an empty Module object as the form or report's Module,
in the same way that setting through the UI does.

And there doesn't seem to be any method to do this in code

(e.g.
'open form in Design view
Set objMod=New Module 'Module object is not "New"-able
Set objFrm.Module=objMod
'save form etc)

So what I've found is that the effect of the code we're both using, for
an object which previously had HasModule=FAlse, is that you then open
the object in the UI, select View/Code or try to access the module in
any other way, and nothing happens. I have to set HasModule to False
and then True again through the UI for the form/report module to be
created.

It's a bit of a pain, but workaroundable.
 
Back
Top