?
Solved

Create ActiveX instances at runtime??

Posted on 2004-10-19
4
Medium Priority
?
1,092 Views
Last Modified: 2011-10-03
I'd like to be able to create as many instances of an OCX as needed at runtime.

I did not write the OCX, and do not have the source code, although I am licensed to use it.

I'm trying to do something like this pseudo-code:
''''''''''''''''''''''''''''''
Dim ocxArray() as TheActiveX

Redim ocxArray(2)

ocxArray(0).Load "C:\myfile"
ocxArray(1).Load "C:\yourfile"
ocxArray(2).Load "C:\ourfile"

...etc....
''''''''''''''''''''''''''''''

I've tried "CreateObject":
Dim junk as Object
Set junk=CreateObject("TheActiveX")
but I get "Runtime error 429 ActiveX can't create object"

I've tried "Controls.Add"
Dim junk as Control
Set junk=Control.Add("TheActiveX", "myname")
but I get "Runtime error 711 Invalid class string Looking for object with ProgID TheActiveX"

Please provide actual working code, not links to someone else's answer.

Thanks.
0
Comment
Question by:wehoit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Expert Comment

by:tonsofpcs
ID: 12352860
If I remember right, its something like this:
Dim x as [activexobjectname]
Set x = New y
[use x]
Set x = Nothing
I'll give it a try when I get a chance and post back.  
0
 
LVL 18

Expert Comment

by:JR2003
ID: 12352886
Try this example:
JR

Option Explicit
' If you are adding an ActiveX control at run-time that is
' not referenced in your project, you need to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton

Private Sub ctlCommand_Click()
   ctlText.Text = "You Clicked the Command button"
End Sub

Private Sub ctlDynamic_ObjectEvent(Info As EventInfo)
   ' test for the click event of the TreeView
   If Info.Name = "Click" Then
      ctlText.Text = "You clicked " & ctlDynamic.object.selecteditem.Text
   End If
End Sub

Private Sub Form_Load()
   Dim i As Integer
   ' Add the license for the treeview to the license collection.
   ' If the license is already in the collection you will get
   ' the run-time error number 732.
   Licenses.Add "MSComctlLib.TreeCtrl"

   ' Dynamically add a TreeView control to the form.
   ' If you want the control to be added to a different
   ' container such as a Frame or PictureBox, you use the third
   ' parameter of the Controls.Add to specify the container.
   Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
                    "myctl", Form1)
   ' set the location and size of the control.
   ctlDynamic.Move 1, 1, 2500, 3500

   ' Add some nodes to the control.
   For i = 1 To 10
      ctlDynamic.object.nodes.Add Key:="Test" & Str(i), Text:="Test" _
                                        & Str(i)
      ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
                           Relationship:=4, Text:="TestChild" & Str(i)
   Next i
   
   ' Make the control visible.
   ctlDynamic.Visible = True

   ' add a textbox
   Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
   ' Set the location and size of the textbox
   ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
                 1, 2500, 100

   ' Change the backcolor.
   ctlText.BackColor = vbYellow

   ' Make it visible
   ctlText.Visible = True

   ' Add a CommandButton.
   Set ctlCommand = Controls.Add("VB.CommandButton", _
                    "ctlCommand1", Form1)

   ' Set the location and size of the CommandButton.
   ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
                    ctlText.Height + 50, 1500, 500

   ' Set the caption
   ctlCommand.Caption = "Click Me"

   ' Make it visible
   ctlCommand.Visible = True
End Sub

0
 
LVL 32

Accepted Solution

by:
Erick37 earned 1000 total points
ID: 12354692
Place one instance of the OCX on your form and give it an Index of 0.  This creates a control array.  During run-time you can load new instances of the control by using the Load keyword.  Example follows:


'Loading new instances of a control at run-time
Dim i As Long

'Get the next index to load
i = TheOCX.UBound + 1

'Load a new instance of the OCX control
Load TheOCX(i)

TheOCX(i).Visible = True
TheOCX(i).Text = "TheOCX " & CStr(i)

'and so on...
0
 
LVL 1

Author Comment

by:wehoit
ID: 12358668
Erick -
Works perfectly!
Thanks.

By the way - I have requested that you should be given partial credit for the other question of mine that you responded to.
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21152405.html

I've posted newer code that's close to 100% accurate - if you want to try for the remaining points....

Again, thanks for a concise, accurate answer.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

719 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