Results 1 to 7 of 7

Thread: Excel Automation Error

  1. #1

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Excel Automation Error

    I'm getting an Automation Error when attempting to use CopyFromRecordset to dump the contents of a recordset created in VB6 to Excel. Here is my code
    VB Code:
    1. Dim oXlApp As Object, oXLWb As Object, oXLWs As Object
    2.  
    3. Dim vRecArray As Variant
    4.     Dim lRecCount As Long
    5.     Dim iFldCount As Integer, iCol As Integer, iRow As Integer
    6.      
    7.     'Create an instance of Excel and add a workbook
    8.     Set oXlApp = CreateObject("Excel.Application")
    9.     Set oXLWb = oXlApp.Workbooks.Add
    10.     Set oXLWs = oXLWb.Worksheets("Sheet1")
    11.    
    12.     'Display Excel and give user control of Excel's lifetime
    13.     oXlApp.Visible = True
    14.     oXlApp.UserControl = True
    15.    
    16.     'Copy field names to the first row of the worksheet
    17.     iFldCount = adoRS.Fields.Count
    18.    
    19.     For iCol = 1 To iFldCount
    20.         oXLWs.Cells(1, iCol).Value = adoRS.Fields(iCol - 1).Name
    21.     Next iCol
    22.  
    23.    [HL="#FFFF80"]oXLWs.Cells(2, 1).CopyFromRecordset adoRS[/HL]
    The error occurs on the highlighted line, and here is the error.
    Attached Images Attached Images  
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel Automation Error

    That looks ok to me.. the only difference between that an the way I normally do it is that I use Range rather than Cells, eg:
    VB Code:
    1. oXLSheet.Range("B15").CopyFromRecordset oRecordset
    ..as Cells is basically a Range object this (theoretically) shouldnt make a difference - but it's worth a go.

    If that doesn't help, what version of Excel are you using, and how is the recordset declared/opened?

  3. #3

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Excel Automation Error

    Quote Originally Posted by si_the_geek
    what version of Excel are you using
    Excel 2003
    Quote Originally Posted by si_the_geek
    and how is the recordset declared/opened?
    VB Code:
    1. strSQL = "SELECT blah blah WHERE blah blah etc "
    2. Set adoRS = New ADODB.Recordset
    3. adoRS.Open strSQL, cn
    cn is a global connection object and adoRS is a global recordset object.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel Automation Error

    I have just used exactly the same code (with Excel 2000) and it works fine.

    Is the recordset declared like this?
    VB Code:
    1. Public adoRS As ADODB.Recordset

    Which version of ADO have you referenced, and which version of MDAC do you have? (see the MDAC link in my signature for a tool to find out).

    Does it work if you use Range instead of Cells?

  5. #5

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Excel Automation Error

    Quote Originally Posted by si_the_geek
    I have just used exactly the same code (with Excel 2000) and it works fine.

    Is the recordset declared like this?
    VB Code:
    1. Public adoRS As ADODB.Recordset
    Yes
    Quote Originally Posted by si_the_geek
    Which version of ADO have you referenced, and which version of MDAC do you have? (see the MDAC link in my signature for a tool to find out).
    Why would MDAC be an issue working between VB6 and Excel 2003?
    Quote Originally Posted by si_the_geek
    Does it work if you use Range instead of Cells?
    No.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Excel Automation Error

    Why would MDAC be an issue working between VB6 and Excel 2003?
    MDAC contains several DB technologies, most notably ADO and database drivers. It is possible that whatever version of ADO you are using (or have available) is not compatible for some reason.

    I would be surprised if it isn't compatible, but the only other thing that I can think of at the moment is the "Display Excel" part of your code should arguably be after the rest of the code - but I dont think that is the problem either.


    On a side note, are you setting your object variables to Nothing after the code runs?

  7. #7

    Thread Starter
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: Excel Automation Error

    Quote Originally Posted by si_the_geek
    On a side note, are you setting your object variables to Nothing after the code runs?
    Well, kinda yes and kinda no.

    The code is there to close and set the objects to nothing, but the program never reaches that point. It blows up well before then
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

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