Passing variables in ActiveX DLL with form

I would like to know if there is a cleaner of way of doing this...

I have an ActiveX (DLL) project that contains a Public Class, a Form and a module. When the class is instantiated, one of the methods of the class is to show the Form, which acts basically as a visual interface to set the properties of the class. Now it's not that hard to access the properties of the class from the Form, but the only way I can see to set the properties in the class from the Form is to use public duplicate variables in a module, which is then accessable from the class. To me, this kind of defeats the object of data encapsulation? Surely there must be a better way to tranfer the values from the Forms' controls back to the Class' properties?

Thanx aloud!
Who is Participating?
bob_onlineConnect With a Mentor Commented:
If you declare an object variable, with events of the form within the class like

Private WithEvents MyForm as frmWhatever

then you can do the following:

1)  In the validate event or lost focus event of each control on the form, raise an event which will be received by the class with the new value which can then set its own property.

2)  Set up properties on the form exposing the values to the class, i.e, Friend Property Get SomeValue() as String.
When the Ok button (or whatever) is clicked on the form, raise an event you have created such as FormClosed.  In the class, when the FormClosed event is received, retrieve the values from the form using the Friend properties.
You could expose the object as in

Public Object as MyClass

and access it as follows

Forget that just re-read the question.

I use the same approach as you are stating.


Public Property Get MyProp() as String
   MyProp = MyObject.MyProp
end Property
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

CD-SoftyAuthor Commented:
bob online...

Clever! However, I can't seem to get it to work! I have done this...


Private WithEvents frm as Form

Public Function ShowProperties()
     Set frm = frmProperties   'this is the form with visual interface.
     frm.Show vbModal          'this works fine!

End Function



Public Event Test(Str as String)

Private Sub cmdOK_Click()
   RaiseEvent Test("Test")
   Unload Me
End Sub


Now for some reason, the Class is not receiving the 'Test' event? I can't access the event proceedure from the drop-down list. I have even tried forcing an event...

Private Sub frm_Test(Str as String)
Msgbox str
End Sub

No joy, any ideas?

Try changing

RaiseEvent Test("Test")
  Unload Me


RaiseEvent Test("Test")
Unload Me

Hope that is more help than the last comment.
Your declaration:
Private WithEvents frm as Form

Should be:
Private WithEvents frm as frmProperties

Now you should be able to select frm from left hand combo on code screen, and event Test from right hand combo
CD-SoftyAuthor Commented:
Thank you to bob online and phildaley, with your combined answers it is now working perfectly. I want to split the award points between you - question is how.

Watch this space.... I'm off to find the answer.

CD-SoftyAuthor Commented:
EE - please help with my previous comment.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.