Results 1 to 4 of 4
  1. #1
    mafrank101 is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2010
    Posts
    12

    Open Excel from Access, sending query info with a parameter

    Gurus,

    I have an Access (2003) query set up that has one filter criteria (Project Number) that it pulls from a form. I have set up a command button that opens an Excel (2007) workbook that then accepts the data from the query, and does some fancy stuff with it.

    I am having difficulty in getting Excel to open properly with the filter criteria from the query. If I remove the filter criteria, it will open Excel just fine, however if I add the filter back to the query, I get an error that states :
    "Too few parameters. Expected 1"

    I realize this is because Excel cannot see the filter criteria from the form, however I have not found how to "preload" this filter into the query before sending it to Excel. The Excel workbook is set up with a connection to this query as well, however if I do a data refresh from Excel, I get the same error.

    Below is the code from the command button in Access. Any help is appreciated.

    Thanks

    Dim appExcel As Excel.Application
    Dim myWorkbook As Excel.Workbook

    Set appExcel = CreateObject("Excel.Application")

    Set myWorkbook = appExcel.Workbooks.Open("C:\Documents and Settings\mfrank\My Documents\Book1.xlsx")


    appExcel.ActiveWorkbook.RefreshAll

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    this does not make sense. or, I don't know how to read it. your code is opening a workbook. it has nothing to do with access. and there is also no "connection" to excel with your code. you are using DAO here, not ADO, which officially creates a connection using the jet engine (I believe). How does your access query relate to your workbook? Are you trying to FILTER your excel records based of off criteria that you have in Access somewhere? Like in your query?

    If that is what you want, you need to use the FILTER object in excel, or the AUTOFILTER tool. You can do both of those via VBA, but you might have to record mouse clicks to figure out how to code it.

    You can also do it through ADO code, and if you do it that way, you can use simple SQL to filter excel records because each sheet is treated like an actual table. That makes it easier of course, but ADO coding is more complex and harder to understand, in general, than DAO code is.

  3. #3
    mafrank101 is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2010
    Posts
    12
    Thanks for your response. Forgive me, as I am a newbie at VBA (SQL developer).

    A user will select a "project number" from a text box in an Access form. Once that is done, my hope was that they could click a button, which would open an Excel workbook that has a connection back to a query in the Access database. This Access query has a filter that uses the value of the "project number" that was entered into the Access form.

    The Excel workbook should just get information about the Project Number that was selected, and then this data is used to create several graphs/pivot tables for analysis. Once the workbook is opened I don't need a dynamic connection between Excel and Access, just a data dump from Access to Excel.

    Essentially, I want my users to enter a project number in Access, click a button, and get a workbook with analysis info about that project.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by mafrank101 View Post
    Essentially, I want my users to enter a project number in Access, click a button, and get a workbook with analysis info about that project.
    You have a few different tools at your disposal. First of all, I don't believe that Access has a "copyfromrecordset" equivalent like excel does.

    so what you want is a data transfer from access to excel. are you familiar with the fact that you can right click the object in access and export it to another program?

    I would suggest you look at these methods in vba:

    docmd.outputto()

    docmd.transferspreadsheet()

    docmd.runimportexport()

    any of those might allow you to export the query you want, and if so, you wouldn't have to deal with the filter in vba because it would already be applied via the access query.

    I am not sure if you can use all of those above methods with an existing workbook. trial and error for that.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Access Tables Open in Excel
    By MikeDBMan in forum Forms
    Replies: 0
    Last Post: 03-31-2011, 02:43 PM
  2. Open a specific Excel application in Access
    By Hobbes29 in forum Programming
    Replies: 1
    Last Post: 02-14-2011, 06:48 PM
  3. VBA to get info from excel
    By cowboy in forum Programming
    Replies: 5
    Last Post: 06-27-2010, 10:55 PM
  4. Fields order changes on sending to excel
    By captgnvr in forum Import/Export Data
    Replies: 1
    Last Post: 10-01-2009, 09:29 AM
  5. Open Excel from within Access?
    By kjw in forum Access
    Replies: 2
    Last Post: 04-08-2008, 07:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums