We help IT Professionals succeed at work.

Returning a VB object in a public function in an ActiveX control

dmclennan asked
I'm Attempting to create a ActiveX OCX with a single textbox object.  Within the OXC, I am defining a public function to expose the properties of the textbox to the calling form.  Code example:

Public Function Get_TextBox() As TextBox
    Set Get_TextBox = Text1
End Function

This results in the following VB compile error:

"Private object modules cannot be used in public object modules as parameters or return types for public procedures, as public data members, or as fields of public user defined types"

Are there any easy ways to expose the Activex objects by using a public function that returns the object without having to code methods that return all of the objects' properties and methods?  
Watch Question

It is possible to do this, but it is not very good idea - we use UserControls to hide 'the internals' and provide the 'black box'.
My suggestion is  not to use UserControl, but use textbox + class, i.e. user will add his own textbox to the form, and class will be used to extend the functionality of the textbox.

That was the suggestion, now, if you still want to expose textbox, there are 2 solutions:

1. use "As Object"

> Public Function Get_TextBox() As TextBox
>     Set Get_TextBox = Text1
> End Function

change to:

Public Function Get_TextBox() As Object
     Set Get_TextBox = Text1
End Function

2. remove function Get_TextBox, and use this line in usercontrol declarations:

Public myTextBox As TextBox

(you'll gain early binding, but it will be 'open', i.e. users will be able to Set that variable)

ad 2. you should also set myTextBox variable to your textbox:
Private Sub UserControl_Initialize()
    Set myTextBox = Text1
End Sub

Here is a small sample if you want to use "textbox + class"
it has autoselect feature

Usercontrol with the same functionality has bunch of delegation code for each property.
Also, it is not easy to change some properties (multiline, scrollbars)
And it has bad performance.

' class clsMyTextBox --------------------------------------
Option Explicit
Public WithEvents TextBox As VB.TextBox

Private Sub TextBox_GotFocus()
   TextBox.SelStart = 0
   TextBox.SelLength = Len(TextBox.Text)
End Sub

Private Sub Class_Terminate()
   Debug.Print "terminate"
End Sub

' Form1, add textbox --------------------------------------
Option Explicit
Private mytextbox As clsMyTextBox

Private Sub Form_Load()
   Set mytextbox = New clsMyTextBox
   Set mytextbox.TextBox = Me.Text1
End Sub

Private Sub Form_Unload(Cancel As Integer)
   Set mytextbox = Nothing
End Sub

It requires some code in declaration section, in Form_Load() and in Form_Unload().
But, it isn't that bad - if you have 10 textboxes, you don't need 10 declarations... sample (uses the same class clsMyTextBox):

' Form1, add many textboxes ---------------------------
Option Explicit
Private mycol As Collection

Private Sub Form_Load()
   Dim x As clsMyTextBox, ctl As Control
   Set mycol = New Collection
   For Each ctl In Me.Controls
       If TypeOf ctl Is TextBox Then
           Set x = New clsMyTextBox
           Set x.TextBox = ctl
           mycol.Add x
       End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
   Set mycol = Nothing
End Sub
Valliappan ANSenior Tech Consultant

ameba, excellent. :)
Ryan ChongSoftware Tead Lead / Business Analyst / System Analyst / Data Engineer

agree with valli_an :)

Thanks, all  :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.