View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Dick Kusleika Dick Kusleika is offline
external usenet poster
 
Posts: 179
Default User forms in VBA

Rich


The problem form is named "fm_DataEntry". The procedure I have in the code
for this problematic form is called "fm_DataEntry_Initialize()". The

(Name)
property of the form is "fm_DataEntry".)


Here's the thing with Userforms. Each Userform has it's own special class
module. When you double click on frm_DataEntry in the VBE, you are opening
this class module. The class module is special (not a normal class module)
because it has a user interface built-in (the actual form) and it has
references set automatically when you add controls to the form. Otherwise
it's just like a class module.

Because each form has it's own class module and each "special userform class
module" only has only one form, MS probably didn't think it was necessary to
put the name of the form in the drop downs. The code in that module can
only relate to one form. You can tell which form's module is showing by
looking at the Application caption (very top of your screen). It should say
frm_DataEntry (Code) so you know in which form you're working.

When I call the form from another procedure within the same project, code
within the Initialize procedure for fm_DataEntry doesn't run. Another odd
point about this is that, within the Code editor, at the top drop-down

boxes
listing the Objects and the Methods, is that:


It won't run under any circumstances because it's not formatted properly for
an event procedure. It should read

Private Sub UserForm_Initialize()

regardless of what you've named your form. Because you can only have one
sub so named in that module and because there can only be one form
associated with that module, then a sub with this syntax will run when the
Initialize event is fired. Any subs named anything else will not run when
that event is fired.

- It appears as "General" in the Objects list and
"fm_DataEntry_Initialize" under the methods drop-down list. (The
disadvantage to this, like I said earlier, is that the code doesn't run. I
think it should appear as "fm_DataEntry" under objects and "Initialize"
under methods.)


The dropdown on the left should show (General) and Userform. The left
should show (Declarations) or the name of each built-in event (e.g. Click,
Initialize, QueryClose). In my Excel, the name of the form doesn't show up
anywhere in those dropdowns.

- If I change the name to from "fm_DataEntry" to "UserForm", then I
have "UserForm" in the Objects list and "Initialize" under the methods

list.
(The advantage here is that it "looks" correct in the drop down boxes at

the
top of the Code editor. I get an error message telling me that the

subscript
is out of range when I try to run my program when I run the code that

calls
the fm_DataEntry form.)


I don't know exactly what's going on here, but my guess would be that you
are naming your form with a keyword, which can confuse the compiler.
Userform is a keyword, Userform1 is not. You also wouldn't want to name
your userform Range or Worksheet for the same reason.

If this doesn't answer your questions, or you have additional questions,
please post back.

--
Dick Kusleika
MVP - Excel
www.dicks-clicks.com
Post all replies to the newsgroup.