CreateObject Function

Named Arguments

No

Syntax

Set objectvariable = CreateObject("library.object"[, servername])


objectvariable

Use: Required

Data Type: Object

A variable to hold the reference to the instantiated object.


library

Use: Required

Data Type: String

The name of the application or library containing the object.


object

Use: Required

Data Type: String

The type or class of object to create.


servername

Use: Optional (Available in VB6 only)

Data Type: String

The name of the server on which the object resides.

Return Value

A reference to an ActiveX object.

Description

Creates an instance of an OLE Automation (ActiveX) object. Prior to calling the methods, functions, or properties of an object, you are required to create an instance of that object. Once an object is created, you reference it in code using the object variable you defined.

Rules at a Glance

  • If your project doesn't include a reference to the object, you must declare the object variable type as Object; this allows the variable to reference any type of object.

  • If an instance of the ActiveX object is already running, CreateObject may start a new instance when it creates an object of the required type.

Example

The following routine defines a generic Object variable, as well as an Excel application object. It then uses the Timer function to compare the performance of the code fragment that uses late binding to instantiate the Excel application object with the one that uses early binding. (For a discussion of late and early binding, see the first item in Section 7.1.6.

Private Sub TestBinding()

Dim dblTime As Double
Dim strMsg As String

' Calculate time for late binding
dblTime = Timer()
Dim objExcelLate As Object
Set objExcelLate = CreateObject("excel.application")
Set objExcelLate = Nothing
strMsg = strMsg & "Late Bound: " & Timer() - dblTime
strMsg = strMsg & vbCrLf

' Calculate time for early binding
dblTime = Timer()
Dim objExcelEarly As Excel.Application
Set objExcelEarly = Excel.Application
Set objExcelEarly = Nothing
strMsg = strMsg & "Early Bound: " & Timer() - dblTime

MsgBox strMsg, vbOKOnly, "Late and Early Binding"

End Sub

Programming Tips and Gotchas

  • The Object data type is the most generic of Visual Basic objects. When an object variable has been defined as type Object, CreateObject performs what is termed late binding. This means that because the precise object type is unknown at design time the object can't be bound into your program when it's compiled. Instead, this binding occurs only at runtime, when the program is run on the target system and the CreateObject function is executed. This need to determine the precise object type by referencing the relevant interfaces at runtime is necessarily time-consuming, and therefore results in poor performance. You can vastly improve performance by using early binding. Early binding necessitates your adding a reference to the required object to your project. You do this in VB by selecting the References option from the Project menu and then selecting the required object from the References dialog. For example, to use the Microsoft Remote Data Objects (RDO) 2.0 library in your project, simply open the References dialog and check the Microsoft Remote Data Object 2.0 option. Then you can create explicit object variables directly, as the following snippet shows:

    Dim rcMyConnection As rdoConnection
    Dim rsMyResults As rdoResultset
    Dim sSQL As String
        
    sSQL = "SELECT * FROM testtable"
        
    Set rcMyConnection = _
        rdoEnvironments(0).OpenConnection("TestServ")
    Set rsMyResults = rcMyConnection.OpenResultset(sSQL)

  • Because your project has a direct reference to the object, it can create the object at the compilation stage. Your program is therefore able to bind references to the object and its OLE interfaces before the object is needed (hence the term early binding), thus improving the performance of the application. If you are unsure of the available objects, methods, events, and properties, you can get complete information from the Object Browser.

  • With the advent of DCOM, the ActiveX object doesn't need to necessarily reside on the computer on which your program is running, although it must always be registered on the computer on which your program is running.

  • VB6 takes the CreateObject function one step further by adding a new parameter, servername. You can now specify the name of the server on which the ActiveX object is registered. This means that you could even specify different servers depending upon prevailing circumstances, as this short example demonstrates:

    Dim sMainServer As String
    Dim sBackUpServer As String
    
    sMainServer = "NTPROD1"
    sBackUpServer = "NTPROD2"
    
    If IsOnline(sMainServer) Then
        CreateObject("Sales.Customer",sMainServer)
    Else
        CreateObject("Sales.Customer",sBackUpServer)
    End If

  • To use a current instance of an already running ActiveX object, use the Get-Object function.

  • If an object is registered as a single-instance object (i.e., an out-of-process ActiveX EXE), only one instance of the object can be created; regardless of the number of times CreateObject is executed, you will obtain a reference to the same instance of the object.

  • It's considered good programming practice (and often a necessity) to tidy up after you have finished using an object by setting objectvariable to Nothing. This has the effect of freeing the memory taken up by the instance of the object, and, if there are no other "live" references to the object, shutting it down. For example:

    Set objectvariable = Nothing

  • For a more in-depth look at creating objects and using them within your application, see Chapter 4.

See Also

GetObject Function, Set Statement, Chapter 4
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.155.185