Instancing a Microsoft Access form
February 20, 2005 8:32 PM   Subscribe

I'm stuck instancing a Microsoft Access form. According to Litwin, Getz and Gilbert (Ch 8), the statement 'Set frm = New Form_frmMyForm' should create a new instance of frmMyForm, referenced via the 'frm' variable. However, when I try this I get a compile error "User-defined type not defined", and it highlights the code 'New frmMyForm'.

I'm using Access 2000, and the form in question contains subforms. Could this be the problem? Is there a workaround? Or is this just more Microsoft Access silliness that I have to program around? I can instance other forms successfully, it's just this one that seems to cause a problem.
posted by Ritchie to Computers & Internet (6 answers total)
For one, how you wrote what you have and how you wrote the part it highlights are wrong (the latter has a space instead of an underscore), maybe you've got it one way and it should be another? The way with the space would make more sense to me.
posted by abcde at 8:55 PM on February 20, 2005

My mistake. It highlights 'New Form_frmMyForm', I just typed it in wrong when I wrote out the question. The problem still remains.
posted by Ritchie at 9:03 PM on February 20, 2005

Where's the declaration of Form_frmMyForm?

It's complaining that it can't see that declaration (definition).

Is the type name "Form_frmMyForm"? (If so, it's a terrible name from an understand/maintenance perspective, but a usual name for an example)

Is the name perhaps "frmMyForm", in which case you want
Set frm = New frmMyForm

Is there perhaps a typo in your textbook?

Do you realize the textbook is talking about an example form -- and not a standard form? Either you defined it yourself, or it's defined in example code that you loaded. Make sure that form definition is in fact accessible.

Did you write a definition for that form? Did you load it?
posted by orthogonality at 10:22 PM on February 20, 2005

Due to the weirdness which is Visual Basic for Applications, no declaration of a particular form is necessary. It is sufficient that the form object has been defined using the graphical form design tools for MS Access to recognise it as a class. All form and report objects in an Access database are automatically classes.

I used frmMyForm just as a way of defining my problem generically: In actuality the form is called 'ATP' and my code reads 'Set frm = New Form_ATP'. The 'Form_' symbol prefixed to the form class name is another example of VBA idiosyncrasy; it tells the compiler (sort of compiler anyway) that I'm trying to instance a form object.

I should point out that this code I'm using works - just not for this particular form, which rules out the possibility of a typo or bad syntax.
posted by Ritchie at 11:44 PM on February 20, 2005

You need to set the "HasModule" property of the form to true. Then the VBA compiler will recognize the form name as the name of a class module.
posted by crunchburger at 6:53 AM on February 21, 2005

Well, bugger me with a fishfork: it worked! You rule, crunchburger!
posted by Ritchie at 11:59 PM on February 21, 2005

« Older Joys of self-denial   |   NetFlix-esque Envelopes Newer »
This thread is closed to new comments.