Link to home
Start Free TrialLog in
Avatar of username1
username1

asked on

An ActiveX control including a form.

Both form1 and usercontrol1 are under project1 in VB5.

From form1, how to access a varible in usercontrol1? Do you think we should not add a form or module when design an ActiveX control? I found we can not simply treat usercontrol1 as a form.....
Avatar of lmorris
lmorris

You're right. A usercontrol is not a form. If you want to pass variables, then you must create properties and pass your information via the properties. If you want to have the control do something, then you must create methods for the control.

Here's an example:

Create a usercontrol project. Place a textbox on the control then place this code in it.

Option Explicit

Public Property Get Value() As Variant
    Value = Text1.Text
End Property

Public Property Let Value(ByVal vNewValue As Variant)
    Text1.Text = vNewValue
End Property

My variable in the user control in this case is a textbox, but it could be anything.

Now add a standard exe project. Place the usercontrol on it and a command button, then place this code in the usercontrol:

Option Explicit

Private Sub Command1_Click()
    MsgBox UserControl11.Value
End Sub

Private Sub Form_Load()
    UserControl11.Value = "123-45-6789"
End Sub

So, to pass a variable, I call it's value property as I did in the form_load proecedure.

To retrieve a variable, I call it's value property as I did in the Command1_click event.

If you need anymore halp on this topic, you can email me at lmorris@netins.net
Avatar of username1

ASKER

Thank you lmorris.

Before we can discuss further, I should point out again that my question is:
How to access a varible in usercontrol1 from form1 which is in the same project as the usercontrol1.
So, there is no exe project!

Cheers.
Hi lmorris,

I am sorry, but I got know the answer to day ;-(
I am going to reopen the quesion.
Thank you again.
Could you explain your problem again, and explain why lmorris' answer doesn't solve it?

I understood your question  exactly as lmorris did, and I think he gave you a good answer.
It doesn't matter that you form is in the same project or not. You still have to create properties and methods by which the form will communicate with your control. The same restrictions apply with class modules.
ASKER CERTIFIED SOLUTION
Avatar of MikeP090797
MikeP090797

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To lmorris:

I think i didn't explained my question clearly, sorry.
1. My form1 and usercontrol1 are in the same project, the one to design a control....so that was usercontrol1 instead of usercontrol11 :-)
2. I have also tried to access a public varible or a created property in usercontrol1 from form1 and without success. The reason has been point out by mikeP.

Cheers.
To lmorris:
To alamo:

Thank you for your comment. Any further comments welcome :-)

I think i didn't explained my question clearly, sorry.

My form1 and usercontrol1 are in the same project, project1, the one to design a control. There was no instance of usercontrol1 yet....That is like declared a type but not yet doing dim the type. So, we can not assign any value to a member of the type :-)

Cheers.
To MikeP:

Thank you very much for your answer and help.
BTW you look don't like ( you said The bottom line is ... ) create a module for holding the public variable in my case. That is what I am doing :-(