PC Review


Reply
Thread Tools Rate Thread

Class Modules and Userforms

 
 
Stephen Lloyd
Guest
Posts: n/a
 
      9th Apr 2009
I'm creating a macro that I want to be more object oriented than procedural.

The macro sends out schedules to employees. The user interacts with a
userform to select the groups to which the schedules should go.

I started by declaring the userform directly from my main sub in the
following fashion.

Dim frmUserForm As FUserformm
Set frmUserForm = New FUserForm

Later, since I wanted to use a class module to encapsulate the information
and functionality I thought I should instead declare the userform in the
class module in a similar fashion as above in order to read values from the
userform into properties in the class module.

i.e. I want to access objSchedules.daynum from the main module where daynum
is determined by a date selected on the userform.

1. Should I declare the userform object in the class module or the main
module?
2. If in the class module, do I also need to declare the class in the
userform module to access properties in the class module? How?
3. If in the main module do I simply set the obj properties using form
properties, i.e.
objSchedules.daynum = frmUserFrom.DayofWeek where DayofWeek has it's own
set of Get and Let procedures and the Let is called from the userforms
OK_Click.

I might have a good cry after this. Thanks for any and all help. I have
read extensively on chip pearsons web site and an article on peltier's
website as well as research on ozgrid and MrExcel. Nothing seems to discuss
interaction between custom class modules and userform(class) modules.
 
Reply With Quote
 
 
 
 
Jon Peltier
Guest
Posts: n/a
 
      10th Apr 2009

"Stephen Lloyd" <(E-Mail Removed)> wrote in message
news:87666BC3-ADAA-47EF-A663-(E-Mail Removed)...
> I'm creating a macro that I want to be more object oriented than
> procedural.
>
> The macro sends out schedules to employees. The user interacts with a
> userform to select the groups to which the schedules should go.
>
> I started by declaring the userform directly from my main sub in the
> following fashion.
>
> Dim frmUserForm As FUserformm
> Set frmUserForm = New FUserForm
>
> Later, since I wanted to use a class module to encapsulate the information
> and functionality I thought I should instead declare the userform in the
> class module in a similar fashion as above in order to read values from
> the
> userform into properties in the class module.
>
> i.e. I want to access objSchedules.daynum from the main module where
> daynum
> is determined by a date selected on the userform.
>
> 1. Should I declare the userform object in the class module or the main
> module?


If the userform is only accessed through the class module, you may as well
declare it only in the class module.

> 2. If in the class module, do I also need to declare the class in the
> userform module to access properties in the class module? How?


I would declare a module-level object variable of the type FUserform. I
would use a property to assign the variable from the class:

'in the userform module:

Dim mclsWhatever as CWhatever

Public Property Set ParentClass (cls As CWhatever)
Set mclsWhatever = cls
End Property

' in the class module:

Dim frmUserForm As FUserformm

Set frmUserForm = New FUserForm
With frmUserForm
Set .ParentClass = Me
.Show
End With

> 3. If in the main module do I simply set the obj properties using form
> properties, i.e.
> objSchedules.daynum = frmUserFrom.DayofWeek where DayofWeek has it's own
> set of Get and Let procedures and the Let is called from the userforms
> OK_Click.


If the form is encapsulated in the class, you would have to use the class as
an intermediary. The main code interacts with data in the class using
properteis in the class, and the class interacts with data in the form using
properties in the form.

> I might have a good cry after this. Thanks for any and all help. I have
> read extensively on chip pearsons web site and an article on peltier's
> website as well as research on ozgrid and MrExcel. Nothing seems to
> discuss
> interaction between custom class modules and userform(class) modules.


If the form doesn't need to interact with the class except at its creation
and destruction (i.e., to get data in and out), just do it with properties,
and put the code into the form that you would other wise put into the class.

- Jon
-------
Jon Peltier, Peltier Technical Services, Inc.
http://PeltierTech.com/WordPress/>
Advanced Excel Conference - Training in Charting and Programming
http://peltiertech.com/Training/2009...00906ACNJ.html
_______


 
Reply With Quote
 
Stephen Lloyd
Guest
Posts: n/a
 
      10th Apr 2009
Jon,

Excellent. Worked like a charm. I had not considered setting the class as
a property. Thanks for sharing the magic.

Stephen

"Jon Peltier" wrote:

>
> "Stephen Lloyd" <(E-Mail Removed)> wrote in message
> news:87666BC3-ADAA-47EF-A663-(E-Mail Removed)...
> > I'm creating a macro that I want to be more object oriented than
> > procedural.
> >
> > The macro sends out schedules to employees. The user interacts with a
> > userform to select the groups to which the schedules should go.
> >
> > I started by declaring the userform directly from my main sub in the
> > following fashion.
> >
> > Dim frmUserForm As FUserformm
> > Set frmUserForm = New FUserForm
> >
> > Later, since I wanted to use a class module to encapsulate the information
> > and functionality I thought I should instead declare the userform in the
> > class module in a similar fashion as above in order to read values from
> > the
> > userform into properties in the class module.
> >
> > i.e. I want to access objSchedules.daynum from the main module where
> > daynum
> > is determined by a date selected on the userform.
> >
> > 1. Should I declare the userform object in the class module or the main
> > module?

>
> If the userform is only accessed through the class module, you may as well
> declare it only in the class module.
>
> > 2. If in the class module, do I also need to declare the class in the
> > userform module to access properties in the class module? How?

>
> I would declare a module-level object variable of the type FUserform. I
> would use a property to assign the variable from the class:
>
> 'in the userform module:
>
> Dim mclsWhatever as CWhatever
>
> Public Property Set ParentClass (cls As CWhatever)
> Set mclsWhatever = cls
> End Property
>
> ' in the class module:
>
> Dim frmUserForm As FUserformm
>
> Set frmUserForm = New FUserForm
> With frmUserForm
> Set .ParentClass = Me
> .Show
> End With
>
> > 3. If in the main module do I simply set the obj properties using form
> > properties, i.e.
> > objSchedules.daynum = frmUserFrom.DayofWeek where DayofWeek has it's own
> > set of Get and Let procedures and the Let is called from the userforms
> > OK_Click.

>
> If the form is encapsulated in the class, you would have to use the class as
> an intermediary. The main code interacts with data in the class using
> properteis in the class, and the class interacts with data in the form using
> properties in the form.
>
> > I might have a good cry after this. Thanks for any and all help. I have
> > read extensively on chip pearsons web site and an article on peltier's
> > website as well as research on ozgrid and MrExcel. Nothing seems to
> > discuss
> > interaction between custom class modules and userform(class) modules.

>
> If the form doesn't need to interact with the class except at its creation
> and destruction (i.e., to get data in and out), just do it with properties,
> and put the code into the form that you would other wise put into the class.
>
> - Jon
> -------
> Jon Peltier, Peltier Technical Services, Inc.
> http://PeltierTech.com/WordPress/>
> Advanced Excel Conference - Training in Charting and Programming
> http://peltiertech.com/Training/2009...00906ACNJ.html
> _______
>
>
>

 
Reply With Quote
 
Jon Peltier
Guest
Posts: n/a
 
      10th Apr 2009
Magic, I don't know. It's just a little trick I stumbled across a long time
ago. I don't remember if I read it somewhere or even thought of it myself.
It's sure useful, though.

- Jon
-------
Jon Peltier, Peltier Technical Services, Inc.
http://PeltierTech.com/WordPress/>
Advanced Excel Conference - Training in Charting and Programming
http://peltiertech.com/Training/2009...00906ACNJ.html
_______


"Stephen Lloyd" <(E-Mail Removed)> wrote in message
news:22692896-43F9-4A61-9242-(E-Mail Removed)...
> Jon,
>
> Excellent. Worked like a charm. I had not considered setting the class
> as
> a property. Thanks for sharing the magic.
>
> Stephen
>
> "Jon Peltier" wrote:
>
>>
>> "Stephen Lloyd" <(E-Mail Removed)> wrote in message
>> news:87666BC3-ADAA-47EF-A663-(E-Mail Removed)...
>> > I'm creating a macro that I want to be more object oriented than
>> > procedural.
>> >
>> > The macro sends out schedules to employees. The user interacts with a
>> > userform to select the groups to which the schedules should go.
>> >
>> > I started by declaring the userform directly from my main sub in the
>> > following fashion.
>> >
>> > Dim frmUserForm As FUserformm
>> > Set frmUserForm = New FUserForm
>> >
>> > Later, since I wanted to use a class module to encapsulate the
>> > information
>> > and functionality I thought I should instead declare the userform in
>> > the
>> > class module in a similar fashion as above in order to read values from
>> > the
>> > userform into properties in the class module.
>> >
>> > i.e. I want to access objSchedules.daynum from the main module where
>> > daynum
>> > is determined by a date selected on the userform.
>> >
>> > 1. Should I declare the userform object in the class module or the main
>> > module?

>>
>> If the userform is only accessed through the class module, you may as
>> well
>> declare it only in the class module.
>>
>> > 2. If in the class module, do I also need to declare the class in the
>> > userform module to access properties in the class module? How?

>>
>> I would declare a module-level object variable of the type FUserform. I
>> would use a property to assign the variable from the class:
>>
>> 'in the userform module:
>>
>> Dim mclsWhatever as CWhatever
>>
>> Public Property Set ParentClass (cls As CWhatever)
>> Set mclsWhatever = cls
>> End Property
>>
>> ' in the class module:
>>
>> Dim frmUserForm As FUserformm
>>
>> Set frmUserForm = New FUserForm
>> With frmUserForm
>> Set .ParentClass = Me
>> .Show
>> End With
>>
>> > 3. If in the main module do I simply set the obj properties using form
>> > properties, i.e.
>> > objSchedules.daynum = frmUserFrom.DayofWeek where DayofWeek has it's
>> > own
>> > set of Get and Let procedures and the Let is called from the userforms
>> > OK_Click.

>>
>> If the form is encapsulated in the class, you would have to use the class
>> as
>> an intermediary. The main code interacts with data in the class using
>> properteis in the class, and the class interacts with data in the form
>> using
>> properties in the form.
>>
>> > I might have a good cry after this. Thanks for any and all help. I
>> > have
>> > read extensively on chip pearsons web site and an article on peltier's
>> > website as well as research on ozgrid and MrExcel. Nothing seems to
>> > discuss
>> > interaction between custom class modules and userform(class) modules.

>>
>> If the form doesn't need to interact with the class except at its
>> creation
>> and destruction (i.e., to get data in and out), just do it with
>> properties,
>> and put the code into the form that you would other wise put into the
>> class.
>>
>> - Jon
>> -------
>> Jon Peltier, Peltier Technical Services, Inc.
>> http://PeltierTech.com/WordPress/>
>> Advanced Excel Conference - Training in Charting and Programming
>> http://peltiertech.com/Training/2009...00906ACNJ.html
>> _______
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Public Variables, Class Modules and Standard Modules Excel Monkey Microsoft Excel Programming 2 30th Apr 2009 03:18 PM
UserForms VBE and Class Modules... MacGuy Microsoft Excel Programming 1 10th Dec 2008 09:21 PM
comboboxes, userforms and class modules natanz Microsoft Excel Programming 3 29th Nov 2005 02:59 PM
Basic question - modules and class modules - what's the difference? Mark Stephens Microsoft Excel Programming 9 8th May 2005 11:48 AM
Delete Modules and Userforms Peter Pantus Microsoft Excel Programming 1 26th Sep 2003 08:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 AM.