+ Reply to Thread
Results 1 to 16 of 16

Pull query results from Access to Excel

  1. #1
    Registered User
    Join Date
    05-21-2007
    Posts
    79

    Pull query results from Access to Excel

    First off, thanks to everyone that's helped me the past week. I've gone from knowing nothing about VBA and thumbing my eyes all day to being able to work consistently on long projects and observe improving results.

    However, I'm at a stand-still again, as my current task calls for a way to communicate between Access and Excel. I know where the data I want in my Excel workbook is, and there's a query in place to pull it into a table. What I'm trying to do is write a macro that will open that particular table in Access, run the query, select the table, open a particular worksheet in my workbook, and then paste the data in.

    This isn't hard to do by hand, besides finding the right database among a mess of others. But I want someone who isn't me to be able to get ahold of the same data by running the macro. It'll make less work for me in the long run.

    Anything would be helpful.

    EDIT: Found this on another website...

    Please Login or Register  to view this content.
    Changing LPath, I can open the right database, but there's always a security warning that throws off the automation. Is there a way around that? I figure this isn't a bad start, I just need a way to, from Excel, run the appropriate query and copy/paste the result.

    EDIT 2: I'm noticing things in the help file about a Xlodbc.xla add-in, but it says not to use it. However, some of the methods in that add-in look like they're what I want to use, like the SQLOpen, SQLExecQuery, and SQLRetrieve functions. What's the reasoning behind the help file saying not to use them (use MS ActiveX Data Objects, ADOs, it says)? Would it be a bad idea to look into this Xlodbc.xla add-in and see if I can use it?
    Last edited by yellephant; 05-29-2007 at 04:28 PM.

  2. #2
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    yellephant,

    This is probably not the ultimate solution to your problem, but I would suggest using the macro recorder as a first step. It will probably have a bunch of stuff that is not needed, but it may give you a place to start by defining a few things for you. I use this a great deal as a starting point to get a few data elements I am needing and then run with it from there.

    I just tried this with an Access database from which I pull data and it definitely gave me one big harry line of information. I can see things within that line that would be needed within a subroutine.

    If the the database is password protected (the one I am using is), the subroutine does stop and ask for the password, but then continues on.

    Again, I realize this is not the answer to your question, but it may give you a place to start.
    Sincerely,
    Jeff

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Do you just want a method for the user to select the correct database, or do you then want to display a list of tables in the database, and then the fields in the selected table?

    Not sure exactly what you are chasing...


    rylo

  4. #4
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    Boylejob: I just tried using the macro recorder, but it doesn't show me any of the steps outside of Excel. From Excel, if I start the recorder, then go find the database I need, open it, run the query, copy the results, and paste them in a new sheet in my Excel workbook, the only steps the recorder catches are those that involve creating a new sheet to paste to, and the paste. And Access doesn't have a macro recording option, so no help there.

    Rylo: I have a database that contains a large amount of information, and I set up a query that pulls a specific subset of that data to a table. I need to perform some calculations on that subset of data and organize it in Excel, so I've got a macro set up right now that can do all that. Except, if the database changes, I need to be able to update it fairly easily.

    To that end, I was looking for a way to write, in Excel, a macro that would open that database in Access, run my query, copy the table that results, and paste it back into Excel. A friend here at work says he knows it can be done, but can't find the code to do it.

    The information I have is the filepath to the database on my network, and the query name to be performed.

    Hope that clears things up a little.

  5. #5
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Assuming you've added a reference to the Microsoft DAO Object Library, this code will open the query called "My_Query" in your database and copy the contents into the active worksheet

    Please Login or Register  to view this content.

    Col
    If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

  6. #6
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    Colofnature: I see how your code plays out, and I think the biggest thing that was holding me back was not knowing what commands were available (things like CopyFromRecordset). However, when I run your code, it errors out because of the Database data type. Does this have to do with the reference to the DAO library you mentioned? If so, how should I go about creating that reference?

    I know that the Access helpfile includes examples using that datatype, but the VBA editor doesn't seem to like it at all.

    EDIT: I found this command through the helpfile, and added it to my previous code.

    Please Login or Register  to view this content.
    This opens the database and runs the query I want. I just don't know how to copy it back.
    Last edited by yellephant; 05-30-2007 at 09:25 AM.

  7. #7
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    If the error is type not defined then it's definitely the reference. To add it, in the VB editor, go to the Tools menu and select References, then in the listbox scroll down to Microsoft DAO x.x Object Library, where x.x is the version number - there might be several, just pick the highest. Check the box next to it, hit OK and you should be sorted.


    Col

  8. #8
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    While it's probably possible to export the data using your method, off the top of my head I don't know how. Besides, the DAO method is more elegant: simpler, runs quicker and doesn't involve opening another app...

  9. #9
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    Alright, I set the reference up, but now it's giving an "application-defined or object-defined error" on the portion of the code that Sets db and rst.

    When I open the database I'm trying to reference, I get a security warning that requires hitting "Open". Could that be a problem?

    The code looks nice, though... everything's getting clearer and clearer.

  10. #10
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Hmm. Can't replicate that error, so I'm at a loss as to how to get round it. If you want you could email me the spreadsheet and database, or a link to them, to <my excel forum user name at gmail dot com>, but due to the firewall around the office network I won't be able to look at it until I get home this evening - maybe another 2 or 3 hours. I'd be happy to see what I can do if you can wait.

    If not, you could go back to your original method and look at the possibilities of "oApp.DoCmd.TransferSpreadsheet". I've never used it myself, but it should allow you to save the results of your query to an Excel file which you can then open in your macro.


    Col

  11. #11
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    I think that error was because I'd copied the code into a sheet object, instead of a module. When I fixed that, it didn't give that error.

    Unfortunately, it gave another one... in the database, the query I want to run calls a function that I've defined in a module in Access. However, when Excel wants to call that function from the Access query, I'm guessing it can't see the module, so it's erroring out there, citing an "undefined function".

    Another friend of mine sent this code...

    Please Login or Register  to view this content.
    Would it be helpful to open a connection to the database or something? I really haven't picked through this code to look up the methods or anything, so I don't know what it's up to.

    Otherwise, is there a way to utilize the Access module functions in Excel?

  12. #12
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    yellephant,

    I apologize! I apparently did not understand exactly what you are doing. My interaction with an Access database is done through Data > Import External Data > New Database Query. That is how I was able to use the recorder because all of it was being done through Excel.

    I am going to bow out of this discussion because there are obviously folks that interact between Access and Excel more than I do and can be a lot more help.

  13. #13
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Apparently not: http://www.access-programmers.co.uk/...1&postcount=10

    Looks like the logic in the Access module will need to be built into your Excel VBA code somehow. What does the Access function do?

  14. #14
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    There are a couple fields in the database that store information in a strange way, so I needed to write translation functions. Like a number with a letter at the end, where the letter represents some other formatting, or a date stored in days rather than months or years like I'd want it.

    I suppose it wouldn't be impossible for me to write another query based of my first one, that simply selected the information from the database in its original form and tabled it, without any reformatting.

    If that were the case, then, would I essentially just use your code the same way, reference the new query, and then have to append a few function calls from Excel to translate the columns? I don't think that would be hard... the functions I wrote in Access to be used in the query are all written in VBA.

  15. #15
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    Yep, either a) create a new query (probably the easiest way if you can alter the database); or b) define your query using SQL. Then do whatever manipulation to the results your Access function would have done. For example

    Please Login or Register  to view this content.
    The two methods of accessing the contents of a field within a record in a recordset called rst are:

    rst!Field_Name - using the bang as a separator and using the name of the field as it appears in the database
    rst.fields(index).value - using the index (0 to fields.count - 1, or the field name as above)

    Of course you'll need to fiddle around with the bit within the Do...Loop to get it to do whatever you need it to but this ought to get you started.

    Col

  16. #16
    Registered User
    Join Date
    05-21-2007
    Posts
    79
    Awesome, awesome work, Col. This is a huge step forward in my work to get this process automated. I've got it pulling in unformatted just fine, and it shouldn't be too hard to code a call of the functions I've already got in Access.

    Again, thanks so much!

+ 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