ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Interfaces for VBA Forms (https://www.excelbanter.com/excel-programming/308650-interfaces-vba-forms.html)

R Avery

Interfaces for VBA Forms
 
I have several forms that I use a lot, and want a simple, clean
interface to access/populate them. What I have done is create a class
that encapsulates the form, and provides only the properties and events
necessary for using the form. However, this was before I found out that
VBA supports interfaces. Would it make more sense to just have an
interface and implement all of the interface code in the form itself, or
continue with the approach I actually used?

If so, one question I would have, would i have to alter my code in the
form to raise the interface's events? For example, button clicks? I am
unlikely to reuse the interface for any other form or class.

Or, is there a better implementation than this?

Any help would be appreciated.

Stephen Bullen[_3_]

Interfaces for VBA Forms
 
Hi R,

Would it make more sense to just have an
interface and implement all of the interface code in the form itself, or
continue with the approach I actually used?


Well, they both work, but I would tend to go for the interface route if you
have multiple forms that can implement the same interface.

If so, one question I would have, would i have to alter my code in the
form to raise the interface's events? For example, button clicks?


Ah. AFAIK, you can't raise an event on an interface from within a class
that implements the interface, so if you want to raise events, you'd be
better to stick with the wrapper class approach you started with.


Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie



R Avery

Interfaces for VBA Forms
 
Thanks. But what you said led me to another question.

Ah. AFAIK, you can't raise an event on an interface from within a class
that implements the interface, so if you want to raise events, you'd be
better to stick with the wrapper class approach you started with.


Interfaces support event, correct? So if a class cannot raise an event
for the interface that it implements, how do the interface events get
raised?

Stephen Bullen[_3_]

Interfaces for VBA Forms
 
Hi R,

Interfaces support event, correct?


No, in VBA6 we can only raise events declared in a class's default
interface - we can't raise events declared in an Implemented interface.

So if a class cannot raise an event
for the interface that it implements, how do the interface events get
raised?


They don't!

That said, if you're willing to hack around the binaries, Matt Curland has
explained how it can be done:
http://groups.google.com/groups?selm...%40tkmsftngp12

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie



R Avery

Interfaces for VBA Forms
 
Interesting... I did not know any of this. Thanks a lot.


Stephen Bullen wrote:

Hi R,


Interfaces support event, correct?



No, in VBA6 we can only raise events declared in a class's default
interface - we can't raise events declared in an Implemented interface.


So if a class cannot raise an event
for the interface that it implements, how do the interface events get
raised?



They don't!

That said, if you're willing to hack around the binaries, Matt Curland has
explained how it can be done:
http://groups.google.com/groups?selm...%40tkmsftngp12

Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie




All times are GMT +1. The time now is 07:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com