Microsoft KB Archive/183691

= How To Access UserControl Properties From the Same Project =

Article ID: 183691

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q183691



SUMMARY
The properties of a UserControl cannot be accessed from other forms or modules in the same project using a syntax such as:

.=

The reference to the UserControl cannot be obtained by specifying the name of the UserControl from forms or modules present in the same project. These properties can be accessed indirectly, however.



Steps to Reproduce Problem
 Create an ActiveX Control project. Click Add Form on the Project menu to add a form to the project.  Add the following code to the UserControl: Public CtlProp as string 'user created property

Private Sub UserControl_Click CtlProp = "passed value" 'set the property Form1.Show End Sub

Private Sub UserControl_Initialize BackColor = vbRed End Sub

  Add the following code to Form1: Private Sub Form_Activate MsgBox Usercontrol1.CtlProp '<--- Error Occurs Here End Sub

 Close all of the open forms in the project.</li> Click Add Project on the File menu to add a Standard EXE to the workspace.</li> Place a copy of your Usercontrol on Form1 of Project2.</li> Press the F5 key to run the project and click on the Usercontrol. When the ActiveX control is clicked and the Form is shown, a run-time error 424 "Object Required" is generated on the line of code that attempts to directly access a property of the Usercontrol.</li></ol>

Workaround
To workaround this problem, you can have the UserControl pass a reference to itself on the Form by a procedure. The following steps illustrate this solution.

<ol> Create an ActiveX Control project.</li> Click Add Form on the Project menu to add a form to the project.</li>  Add the following code to the UserControl: Public CtlProp As String 'user created property

Private Sub UserControl_Click CtlProp = "passed value" 'set the property Load Form1 Call Form1.ControlRef(Me) 'pass the reference before showing Form1.Show End Sub

Private Sub UserControl_Initialize BackColor = vbRed End Sub

</li>  Add the following code to the Form: Dim cCtl As UserControl1

Private Sub Form_Activate MsgBox cCtl.CtlProp 'this works fine now End Sub

Sub ControlRef(cC As UserControl1) Set cCtl = cC     End Sub

</li> Close all of the open forms in the project.</li> Click Add Project on the File menu to add a Standard EXE to the workspace.</li> Place a copy of your Usercontrol on Form1 of Project2.</li> Press the F5 key to run the project. Click on the Usercontrol. No error occurs.</li></ol>

Additional query words: user defined properties access refer kbCtrlCreate kbControl kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport

Keywords: kbhowto kbgrpdsvb KB183691

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.