Solved

An ActiveX control including a form.

Posted on 1998-07-25
9
164 Views
Last Modified: 2010-05-03
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.....
0
Comment
Question by:username1
9 Comments
 
LVL 2

Expert Comment

by:lmorris
ID: 1466640
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
0
 

Author Comment

by:username1
ID: 1466641
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.
0
 

Author Comment

by:username1
ID: 1466642
Hi lmorris,

I am sorry, but I got know the answer to day ;-(
I am going to reopen the quesion.
Thank you again.
0
 
LVL 6

Expert Comment

by:alamo
ID: 1466643
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.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Expert Comment

by:lmorris
ID: 1466644
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.
0
 
LVL 8

Accepted Solution

by:
MikeP090797 earned 70 total points
ID: 1466645
You simply can't do it. A UserControl is more simular to a class then to a form, it has instances - it means you have to create an instance of the control, like to place it on a form so you can access it's variables. For instance, if you have a class named Class1, you can't do Class1.Var=X, you must create a variable of that type:
Dim C as class1
C.Var=X
The same thing happens with UserControls.

The bottom line is if you want to share variables between form and usercontrol, you must create a module and declare the variables as Public, to make them global through the project
0
 

Author Comment

by:username1
ID: 1466646
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.
0
 

Author Comment

by:username1
ID: 1466647
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.
0
 

Author Comment

by:username1
ID: 1466648
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 :-(
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now