DOCUMENT:Q183691 13-AUG-1998 [vbwin] TITLE :HOWTO: Access UserControl Properties From the Same Project PRODUCT :Microsoft Visual Basic for Windows PROD/VER: OPER/SYS:WINDOWS KEYWORDS: ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0 --------------------------------------------------------------------- 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. MORE INFORMATION ================ Steps to Reproduce Problem -------------------------- 1. Create an ActiveX Control project. 2. Click Add Form on the Project menu to add a form to the project. 3. 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 4. Add the following code to Form1: Private Sub Form_Activate() MsgBox Usercontrol1.CtlProp '<--- Error Occurs Here End Sub 5. Close all of the open forms in the project. 6. Click Add Project on the File menu to add a Standard EXE to the workspace. 7. Place a copy of your Usercontrol on Form1 of Project2. 8. 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. 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. 1. Create an ActiveX Control project. 2. Click Add Form on the Project menu to add a form to the project. 3. 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 4. 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 5. Close all of the open forms in the project. 6. Click Add Project on the File menu to add a Standard EXE to the workspace. 7. Place a copy of your Usercontrol on Form1 of Project2. 8. Press the F5 key to run the project. Click on the Usercontrol. No error occurs. Additional query words: user defined properties access refer kbCtrlCreate kbControl kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport ====================================================================== Platform : WINDOWS Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.