Results 1 to 5 of 5

Thread: CreateObject

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Posts
    111

    Question

    Can anyone tell me what the difference is between these two codes?

    Code1
    Code:
    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    Code2
    Code:
    Dim rs
    Set rs = CreateObject(blah) 'not too sure how to use this function

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    With the first one, you already have a reference in place.
    You add in the reference from the properties menu in vb. Setting a reference here, lets you type in the code as in your first example.

    The second one is you creating and setting up the reference to the object via code. This is useful as with vbscript for example - when you're using notepad, you don't have a nice project menu to do all this for you.
    With the createobject method, as you create the reference, there's no need for the New keyword as it's automatically a new reference of the object.

    So -
    Project > Properties > microsoft Excel 8.0 object
    is just getting microsoft to do the following for you -
    set ObjXls = createobject("Excel.Application")

    Hope I made some sense there !
    Last edited by alex_read; Apr 19th, 2001 at 05:04 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Posts
    111

    Thumbs up

    Ahh, I get it now, thanks alex!

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Also, setting a reference (early-binding) is much faster than using CreateObject at runtime(late-binding).
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2001
    Posts
    111
    Yeah, I had a feeling that came in to it. Thanks also.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width