Click to See Complete Forum and Search --> : Calling methods of Dynamically Created ActiveX Control.


July 27th, 1999, 01:22 AM
I have created a usercontrol which has a Function Display which simply returns a string. I could instantiate the Activex control by using the following code

Dim ctrl As Control
Dim str As String
Set ctrl = Me.Controls.Add("DispProj.DispControl", "Dispproj")
ctrl.Visible = True

str = ctrl.Display ' this line gives the error-438

But when I try to call the Display method I am getting a Runtime Error-438
(ErrDescription:The object doesn't support this method or property). Then
I tried to call Display method by using the "CallByName" function, but it didn't work.

Finally I attemped to create the Control Dynamically by using the CreateObject function. CreateObject is creating the instance of the object but it is not showing the control on the form. But surprisingly I could call the Display method.

Please help me.

vmkrish
September 8th, 1999, 04:57 AM
Dynamically adding control(Not Control Array) to a form is one of the new feature of VB6. In this case we Use
ME.Controls.Add
to add control to the form.
Here we are storing Reference to the Control in a Variable.

So to Invoke A Method of this control,which is dynamically Created Use the Statement
CallByName
Which is new in VB6.

Refer MSDN for syntax of CallByName.

Regards,


VMK

(Sent Your comments to vmkrish@yahoo.com)

Vinayak Sapre
September 12th, 1999, 01:52 PM
That is correct. You can not call the display method of the control. You are getting reference of the correct object but you assigning it to ctl, which is a variable of the type control. Display is not method of class "control" however visible is a property of class "control". So you get that error for method but not for property.

Dim obj as Object
Dim ctrl As Control
Dim str As String
Set ctrl = Me.Controls.Add("DispProj.DispControl", "Dispproj")
ctrl.Visible = True

str = ctrl.Display() ' error not method of class Control

str = CallByName (ctrl, "Display", VbMethod) ' should work

Set obj = ctrl
str = obj.Display() ' will also work & same as call by name.

Set obj = nothing ' will not destroy control. reduce reference count.