Creating the ActiveX Controls Dynamically in Code


The ActiveX edition controls do not need to be placed on a form at design time. They can also be created in code, which allows an application to create instances only when they are needed (e.g., one instance for each connection), and to use the controls in code modules, classes, and environments that do not have forms.

There are two ways to create the controls in code: early binding, which supports events, and late binding, which does not require a reference to the control.

Early Binding

To use early binding, first add a reference to the type library of the control (e.g., /n software inc. - IPWorks 2026 HTTP Control). In Visual Basic 6, references are added in Project > References, and in VBA (e.g., in Excel or Access) in Tools > References of the Visual Basic editor.

The control can then be declared using its class name, and created using New. To handle the events of the control, declare it using WithEvents:

Dim WithEvents http1 As HTTP

Private Sub Command1_Click()
  Set http1 = New HTTP
  http1.Get "https://www.example.com/"
  Debug.Print http1.TransferredData
End Sub

Private Sub http1_Status(HTTPVersion As String, StatusCode As Integer, Description As String)
  ' Print the status line returned by the server.
  Debug.Print StatusCode & " " & Description
End Sub

Variables declared using WithEvents must be declared at module level in a form, a class module, or (in VBA) a document module such as ThisWorkbook, and cannot be declared in a standard module. After declaring the variable, the event handlers can be added by selecting the variable and the event in the drop-down lists at the top of the code window.

Late Binding

To use late binding, declare the variable as Object, and create the control using CreateObject with its ProgID. The ProgID of each control is IPWorks. followed by the name of the control (e.g., IPWorks.HTTP), and a version-specific ProgID (e.g., IPWorks26.HTTP) is also registered.

Dim http1 As Object
Set http1 = CreateObject("IPWorks.HTTP")
http1.Get "https://www.example.com/"
Debug.Print http1.TransferredData

Late binding does not require a reference to the type library, and can also be used from scripting languages such as VBScript. However, events cannot be handled using late binding, so it is best suited to components whose methods do not depend on events (e.g., the Get method of the HTTP control, which returns the response in the TransferredData property).

Licensing

In Visual Basic 6, the license is embedded in the application automatically when it is compiled. In other environments, each instance of a control that is created in code must be licensed by assigning a runtime license to its RuntimeLicense property:

http1.RuntimeLicense = "RUNTIME_LICENSE"

For more information, please see the readme file included in the installation.

32-bit and 64-bit Applications

The controls are included in 32-bit (in the lib folder of the installation) and 64-bit (in the lib64 folder) versions, e.g., http260.ocx. The version of the controls that is used must match the application, e.g., 64-bit versions of Microsoft Office require the 64-bit controls, while Visual Basic 6 applications always use the 32-bit controls.

When the application is deployed, the .ocx file of each control it uses must be deployed and registered on the target machine (e.g., using regsvr32 http260.ocx). If a control is not registered, or is only registered for the other architecture, CreateObject and New fail with error 429 (ActiveX component can't create object).

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.