+ Reply to Thread
Results 1 to 45 of 45

DAO to pull an Access Query into Excel

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

    DAO to pull an Access Query into Excel

    How would I set up via DAO to pull an Access Query into Excel? The reason I am wanting to use DAO is because in the query there is a LARGE memo field, and any other way I try to bring in the query it cuts off the Memo field.
    Last edited by jo15765; 01-19-2012 at 11:54 AM.

  2. #2
    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: DAO to pull an Access Query into Excel

    Here is a very simple example

    Please Login or Register  to view this content.
    It requires you to set a reference to the DAO library.
    Good luck.

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

    Re: DAO to pull an Access Query into Excel

    The query string..

    Could I pull that directly from the SQL view from Access?

  4. #4
    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: DAO to pull an Access Query into Excel

    Just use the name of the view/query.

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

    Re: DAO to pull an Access Query into Excel

    That imports the data, but it is still cutting my memo field off...

  6. #6
    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: DAO to pull an Access Query into Excel

    Assuming you are not grouping on the memo field in your query, I suspect you may have to loop through the data rather than using CopyFromRecordset. GetRows may be an option, but you need to be aware that the resulting array is transposed from what you might expect.

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

    Re: DAO to pull an Access Query into Excel

    I am not grouping on anything. The only parameter other than "Not Null" I am using is, I am sorting Date descending. How would I loop through the data?
    Last edited by jo15765; 01-18-2012 at 10:34 AM. Reason: Used wrong Term -- Am not grouping only sorting

  8. #8
    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: DAO to pull an Access Query into Excel

    Roughly

    Please Login or Register  to view this content.
    or populate an array with the data and write that out to the sheet. If you have a lot of data it will be much faster.

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

    Re: DAO to pull an Access Query into Excel

    I think the largest that the query gets is 200 records.

    Where would I add the code you posted above?
    Last edited by jo15765; 01-18-2012 at 10:38 AM.

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

    Re: DAO to pull an Access Query into Excel

    it cuts off the Memo field
    What do you mean: is not visible ? or 'stops the importing process' ?
    Excel only shows a limited amount of characters in a cell, while that cell contains much more.
    You can check that using msgbox len(sheet1.cells(1))



  11. #11
    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: DAO to pull an Access Query into Excel

    Quote Originally Posted by jo15765 View Post
    I think the largest that the query gets is 200 records.

    Where would I add the code you posted above?
    It replaces the
    Please Login or Register  to view this content.
    line.

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

    Re: DAO to pull an Access Query into Excel

    The memo field may actually contain 500 characters, while Excel only imports 250 (of course those numbers are not accurate just using for examples sake)

    The way I have been importing the data, is copying from the query, right clicking in Excel, choosing paste special, then selecting CSV and it pastes all characters from the Memo field.

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

    Re: DAO to pull an Access Query into Excel

    Quote Originally Posted by OnErrorGoto0 View Post
    It replaces the
    Please Login or Register  to view this content.
    .
    WHen I use this code...I get variable not defined on j and n --- My question is what data type should they be? I defined them as String and then received a type mismatch on N

  14. #14
    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: DAO to pull an Access Query into Excel

    They should both be Long data types. Apologies - I omitted the declaration lines from the top of my code sample.

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

    Re: DAO to pull an Access Query into Excel

    I get a debug error of invalid operation on
    Please Login or Register  to view this content.

  16. #16
    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: DAO to pull an Access Query into Excel

    Ah - remove that line - I forgot I was using a forward only recordset.

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

    Re: DAO to pull an Access Query into Excel

    Argh, it's still not pulling inn all the data from my memo field in access.

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

    Re: DAO to pull an Access Query into Excel

    I don't think you can loop through datasets with this:
    Please Login or Register  to view this content.
    Try changing it to:
    Please Login or Register  to view this content.

  19. #19
    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: DAO to pull an Access Query into Excel

    Does the query do anything to the memo field other than return it?

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

    Re: DAO to pull an Access Query into Excel

    oops was looking at the wrong page :s

  21. #21
    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: DAO to pull an Access Query into Excel

    Quote Originally Posted by Kyle123 View Post
    I don't think you can loop through datasets with this:
    Please Login or Register  to view this content.
    Try changing it to:
    Please Login or Register  to view this content.
    You can - but only once.

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

    Re: DAO to pull an Access Query into Excel

    Ah right ok, my DAO is pretty limited I use ADO

  23. #23
    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: DAO to pull an Access Query into Excel

    Should be the same in ADO. If you use a firehose cursor for example, it's quick but you can only iterate the records once.

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

    Re: DAO to pull an Access Query into Excel

    It only returns the memo field. The only thing that could be considered quirky is the fact that I have date sorted descending.

  25. #25
    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: DAO to pull an Access Query into Excel

    I would probably need to see the database but it may also be a limitation of Office 2000, which I do not have.

  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: DAO to pull an Access Query into Excel

    @kyle123

    In other news, your avatar has brightened my day considerably. A little like Baloo after a serious diet.

    Just noticed: thank you for the rep, by the way.
    Last edited by OnErrorGoto0; 01-18-2012 at 12:53 PM.

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

    Re: DAO to pull an Access Query into Excel

    db1.zipdb1.zipLet me attach a database with garbage data, so you can test

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

    Re: DAO to pull an Access Query into Excel

    @OnError brightens mine too, it frequently needs brightening


    This gave me the full text of the memo field:

    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: DAO to pull an Access Query into Excel

    I am getting Variable Not defined on
    Please Login or Register  to view this content.

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

    Re: DAO to pull an Access Query into Excel

    Try changing it to 3

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

    Re: DAO to pull an Access Query into Excel

    Now I am getting a debug error of Uknown is not a valid path Highlighting the cn.Open

    Just to make sure I have it correct...
    Please Login or Register  to view this content.
    Last edited by jo15765; 01-18-2012 at 03:57 PM. Reason: Forgot the closing code tag

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

    Re: DAO to pull an Access Query into Excel

    You need to change the file path to where your db is as well as the name of your db

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

    Re: DAO to pull an Access Query into Excel

    desired result with:

    Please Login or Register  to view this content.
    or using 'early binding':

    Please Login or Register  to view this content.
    Last edited by snb; 01-18-2012 at 04:38 PM.

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

    Re: DAO to pull an Access Query into Excel

    I had the Default Directory mapped wrong. I forgot one stipulation...how would I add into it where Date is Not null?

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

    Re: DAO to pull an Access Query into Excel

    SNB -- the copy recordset option would always cut off my memo field in the database, so at least the times I have tested it, option would not work.

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

    Re: DAO to pull an Access Query into Excel

    You didn't test my code, because I got the full 'comment' from your database: 475 characters long, what will be shown in the messagebox.
    The crux is in adding the 3 in the instruction 'open' of the recordset.

    Please Login or Register  to view this content.
    Last edited by snb; 01-18-2012 at 05:47 PM.

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

    Re: DAO to pull an Access Query into Excel

    I get a compile error of variable not defined on the co1 line of your code snb

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

    Re: DAO to pull an Access Query into Excel

    Yes, of course: remove 'Option Explicit'

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

    Re: DAO to pull an Access Query into Excel

    It's still cutting off the data in my memo field using that code.

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

    Re: DAO to pull an Access Query into Excel

    So how come ? cfr. the attachment.

    And what is being shown in the message Box ?
    Attached Files Attached Files

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

    Re: DAO to pull an Access Query into Excel

    The message box shows
    14

    Troyb@nist.gov

    what is cfr the attachment?

    That's very peculiar that my sample I provided works, but when I try to run it on my real database it doesn't....

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

    Re: DAO to pull an Access Query into Excel

    check in the real database what is the content of the 'comment' field in the first record.

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

    Re: DAO to pull an Access Query into Excel

    I am not sure what you mean by content...It is a comment, just a long typed out expression similar to what I provided in the sample database that was uploaded.

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

    Re: DAO to pull an Access Query into Excel

    I just figured out what the problem is. In the query it is set up to show: FullComments: "Customer Comments:" & [comment] And it names the field FullComments (which can drop I don't care about that) and then it appends Customer Comments: to the beginning of the actual comments in the database. I need to keep Customer Comments: as a prefix for the comment field, but it is clearly messing up the DAO/ADO import. Any suggestions on a workaround for this?

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

    Re: DAO to pull an Access Query into Excel

    I just wrote a function that would append "Customer Comments:" To the beginning of that column, so that is figured out now. The problem I have now is that Rows 1 and 2 are header rows, and each of the codes provided start pulling the data onto Row 2. What would need to be modified so that data does not begin until Row 3?

+ 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