+ Reply to Thread
Results 1 to 45 of 45

Excel DAO import not working

  1. #1
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Excel DAO import not working

    I am running this code to import from about 20 different databases. It works fine on all but 2!! This is the debug error that I get.
    Run Time error '-214767259 (80004005)'
    Method 'CopyFromRecordset' of Object 'Range' failed


    And here is the code that I am running, I checked the database name, the file path and the query name and they are all correct, so I am at a loss as to what caused this to only go wrong on 2!
    Please Login or Register  to view this content.

    Edit:

    It is also the exact same query from the other databases (of course just pulling from a different table) and this query will run w/o issues, if I open the database and run it, but for some reason the DAO just isn't taking or something like that. Also, the workbook has 2 sheets so that is not causing the issue either.
    Last edited by jo15765; 02-06-2012 at 12:31 PM. Reason: Addional Info

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Excel DAO import not working

    A a guess there are 2 reasons this could occur, firstly you are returning more rows/columns than your spreadsheet has. Secondly, your recordset contains certain OLE object fields that you can't import into Excel, from MSDN:
    Note When using CopyFromRecordset, you should be aware that the ADO or DAO recordset you use cannot contain OLE object fields or array data such as hierarchical recordsets. If you include fields of either type in a recordset, the CopyFromRecordset method fails with the following error:
    Run-time error -2147467259:
    Method CopyFromRecordset of object Range failed.
    p.s Also you are using ADO, not DAO

  3. #3
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Just an additional point: you might find debugging considerably easier if you used some variables that you could inspect in the locals window (I realise the author prefers to simply write the shortest code possible).
    Good luck.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Excel DAO import not working

    Indeed, that snippet has snb written all over it

  5. #5
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    A little surprising though, as the Connection object seems to serve no purpose there (as the connection string is being passed to the Recordset's Open method, not the Connection object) and removing it would save four lines.

  6. #6
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Excel DAO import not working

    What version of Excel are you using OP? Is it xl2000 as listed in your profile or an earlier version?

  7. #7
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by Kyle123 View Post
    A a guess there are 2 reasons this could occur, firstly you are returning more rows/columns than your spreadsheet has. Secondly, your recordset contains certain OLE object fields that you can't import into Excel, from MSDN
    One of the query's is returning 135 rows of data, and the other is returning 211...I don't feel like that is to much for Excel to handle? Also, it is the exact query that is being run in other datbases as well, and it can pull those results with no issues?

  8. #8
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by Firefly2012 View Post
    What version of Excel are you using OP? Is it xl2000 as listed in your profile or an earlier version?
    Yes I am running Excel 2000

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Excel DAO import not working

    Then as I mentioned above it could be what is being stored in the database. Is there anything different stored in the tables that aren't working than the tables that are, for example images etc

  10. #10
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Excel DAO import not working

    Is it returning more than a single column of data? I would think in that case you would need to modify to:

    Please Login or Register  to view this content.
    However, I haven't tested that specifying the whole row might cause a problem (don't have Access).

  11. #11
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by Kyle123 View Post
    Then as I mentioned above it could be what is being stored in the database. Is there anything different stored in the tables that aren't working than the tables that are, for example images etc
    No there is only text stored in the table. No pic's, hyperlinks, or anything like that.

  12. #12
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Is any of the text longer than say 911 characters?

  13. #13
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by Firefly2012 View Post
    Is it returning more than a single column of data? I would think in that case you would need to modify to:

    Please Login or Register  to view this content.
    However, I haven't tested that specifying the whole row might cause a problem (don't have Access).
    It is returning whatever the query holds, it may be one row, or up to 200 (which could grow, that is the highest number as of now).

  14. #14
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by OnErrorGoto0 View Post
    Is any of the text longer than say 911 characters?
    Yes. There is a memo field that is just Notes that are entered, and sometimes those can get very very lengthy.

  15. #15
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    CopyFromRecordset will fail with that much text. You will need to loop I think for that.

  16. #16
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    DOH! Can someone show me an example of a loop I could use that would pull in the data?

  17. #17
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Something akin to this (untested)
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    In one database the code I was using pulled in the memo field and it has 1100 characters in it?! Strange!!!!

    I will test the above code, thank you.

  19. #19
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by OnErrorGoto0 View Post
    Something akin to this (untested)
    Please Login or Register  to view this content.
    Running the above code, I get a RunTime Error 7 'Out of Memory' after it pulls in about 45 of 135 rows needed for the complete query

  20. #20
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    I do not know why that might be, I'm afraid, and do not have 2000 to test with.

  21. #21
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Possibly then bc this PC I am on is a huge POC! Thank you for the help!

  22. #22
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    That could be - plus Excel 2000 was extremely limited in the amount of memory it could use.

    On the other hand, the fact that both methods fail on this particular query could be an indication of another issue. Do you have any error values in the query?

  23. #23
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Error Values?

  24. #24
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Yes. For example if your query does calculations and you try to divide by zero, you get an error as the result.

  25. #25
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Ah okay. No there is nothing of that sort in the query. It is returning just basic data, like Name Address Phone # etc etc, no calculations or anything like that.

  26. #26
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Oh well.

    Can you just test this version
    Please Login or Register  to view this content.

  27. #27
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    On this line
    Please Login or Register  to view this content.
    I get a compile error of invalid or unqualified reference.

  28. #28
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Apologies - I rewrote some of it at the last minute. That should be
    Please Login or Register  to view this content.

  29. #29
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Still getting an Out of Memory error. It will highlight this line of code:
    Please Login or Register  to view this content.

  30. #30
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    What are the values of X, y and vaData(x, y) at the time?

  31. #31
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    x = 62
    y = 24

  32. #32
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Revised version of the main code with an added error handler just to see how long the data is when the error occurs
    Please Login or Register  to view this content.

  33. #33
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    I am getting a type mismatch error on this line:
    Please Login or Register  to view this content.

  34. #34
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Can you change that to
    Please Login or Register  to view this content.
    to see what the data is? I'm curious about the Type Mismatch there.

  35. #35
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Excel DAO import not working

    Another way of checking:

    stConn =

    Please Login or Register  to view this content.
    Now you can see what has been imported from the database.
    Last edited by snb; 02-06-2012 at 11:49 AM.



  36. #36
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by OnErrorGoto0 View Post
    Can you change that to
    Please Login or Register  to view this content.
    Still a type mismatch...In my limited coding knowledge, could it be because we have x and y declared as long and in that piece of code we are trying to make them be a string??

  37. #37
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    No, but it implies that whatever you are importing cannot be converted to a string, which probably explains why all the code is failing.
    How about
    Please Login or Register  to view this content.
    You can also test snb's code, though I suspect it may fail for the same reason.

  38. #38
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Quote Originally Posted by snb View Post
    Another way of checking:

    stConn =

    Please Login or Register  to view this content.
    Now you can see what has been imported from the database.
    It is highlighting
    Please Login or Register  to view this content.
    And telling me variable not defined.

  39. #39
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    Add
    Please Login or Register  to view this content.

  40. #40
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Going back to this line of code
    Please Login or Register  to view this content.
    I get a msgBox that shows
    Out of Memory
    Current Item Length = 1094

  41. #41
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    I thought you were getting a type mismatch there?

    I cannot see an issue with 1094 characters unless perhaps it starts with an = or other operator symbol? (1024 is the maximum formula length)

  42. #42
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    It was my silly fault, I left out the "." between the database name and mdb. Let me check all the entries in the database and see if someone type in a crazy character or something that is throwing it.

    Could the field beginning with a "-" be causing the error? It is interpreting it as a calculation?
    Last edited by jo15765; 02-06-2012 at 12:16 PM.

  43. #43
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Excel DAO import not working

    I vaguely remember that having "ID" in the first field in the first record creates problems (MS has an article about it).

  44. #44
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Excel DAO import not working

    Removing the "-" from the beginning of the fields is now letting the query pull in the data as needed. Thanks guys for the continued support, and patience.

  45. #45
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Excel DAO import not working

    You could also just prefix with an apostrophe in the code
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1