How to pass values from ActiveX Control to Standard exe

Posted on 2002-05-08
Last Modified: 2010-05-02
I've just created a simple ActiveX, say, UserControl1 by using VB 6 sp5, and then I registered the ocx file by using regsrv32.

Next, I created a new EXE project and then add that ActiveX with three textfields namely TextBox1, TextBox2 and TextBox3. Finally, I created three textfields on my newly EXE project, namely txtTextBox1, txtTextBox2 and txtTextBox3 and a button.

After clicking the button, the values of the textfields on the ActiveX Control will be copied to those on the EXE one, say,

txtTextBox1.Text = TextBox1.Text
txtTextBox2.Text = TextBox2.Text
txtTextBox3.Text = TextBox3.Text

But, what's the syntaxt then? After I typed "UserControl1", I couldn't see any TextBox1, TextBox2, TextBox3 on the selection.

Thanks for the help!
Question by:mscproj
  • 2
LVL 43

Expert Comment

ID: 6995915
You haven't created any properties that are exposed to the container application. There is an add-in activex control wizard that will automate some of this for you. I would suggest that as you are starting with this you use the wizard to create the properties and link them to the underlying properties of the controls that you have placed in the usercontrol. Then examine the resulting code to understand how properties can be employed to do this. The simplest property method(s) would be something like this:

Public Property Let TB1(ByVal TBText As String)
  Textbox1.Text = TBText
End Property

Public Property Get TB1() As String
  TB1 = Textbox1.Text
End Property


Expert Comment

ID: 6995950
Don't forget to use the Usercontrol_ReadProperties and
Usercontrol_WriteProperties, there you can store and retrieve the values in the PropertyBag, which means you don't lose the values after closing the project, maybe not necessary in this case though.

Author Comment

ID: 6996011
Yes, I see. But, if I create the ActiveX control, and want to build a simple exe project that has two that ActiveX Control.

After I enter the values on the three textfields on 1st ActiveX Control, then enter a button, it will update those three textfields on 2nd ActiveX Object.

Also, after I change the valueson the three textfields on the 2nd ActiveX Control, then enther another button, it will update those three textfields on the 1st Active Object.

How to write the coding the those two button events?

I've written something like

Private Sub CommandButton1_Click()
   MainControl2_getText1 (MainControl1.getText1)
   MainControl2_getText2 (MainControl1.getText2)
   MainControl2_getText3 (MainControl1.getText3)
End Sub

Private Sub CommandButton2_Click()
   MainControl1_getText1 (MainControl2.getText1)
   MainControl1_getText2 (MainControl2.getText2)
   MainControl1_getText2 (MainControl2.getText3)
End Sub

My ActiveX Control is something like this:

Public Property Get getText1() As String
   getText1 = TextBox1.Text
End Property

Public Property Let getText1(ByVal vNewValue As String)
   TextBox1.Text = vNewValue
End Property

Thanks again!
LVL 43

Accepted Solution

TimCottee earned 100 total points
ID: 6996032
Actually it would be:

Private Sub CommandButton1_Click()
  MainControl2.getText1 = MainControl1.getText1
  MainControl2.getText2 = MainControl1.getText2
  MainControl2.getText3 = MainControl1.getText3
End Sub

Private Sub CommandButton2_Click()
  MainControl1.getText1 = MainControl2.getText1
  MainControl1.getText2 = MainControl2.getText2
  MainControl1.getText2 = MainControl2.getText3
End Sub

As a property is assigned using the = operator e.g., PropertyName = PropertyValue as with the intrinsic properties of objects.

You can also simplify this slightly using a with .. end with block:

Private Sub CommandButton1_Click()
  With MainControl2
    .getText1 = MainControl1.getText1
    .getText2 = MainControl1.getText2
    .getText3 = MainControl1.getText3
  End With
End Sub

Private Sub CommandButton2_Click()
    .getText1 = MainControl2.getText1
    .getText2 = MainControl2.getText2
    .getText2 = MainControl2.getText3
  End With
End Sub

This reduces the number of times that VB has to resolve the property of the object as it already gains a reference to the main object in the With statement and can then identify all properties and methods of that object without having to do this in each subsequent line. In some circumstances this can increase processing speed and is a recommended practice where possible.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

726 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