Results 1 to 6 of 6
  1. #1
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67

    How To Use An SQL Statement In A TransferSpreadsheet Command


    I have some some data i would like to output to an excel ss using a SQL statement in my code. Problem is, I don't know how to refer to that sql statement in the transferspreadsheet statement.

    Below I have an example. The strSQL variable in the transferspreadsheet statement is obviously incorrect, but just placed to show what I want..

    I know I could do it by creating a table and then referring to that created table in the transferspreadsheet function, but would like avoid this if possible..

    Any assistance is appreciated..

    strSQL = "SELECT table1.SUBID from Table1 where table1.loc = somecriteria"

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, strSQL, filepathandname, True

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    others might know better, but I think you need an actual object to do it with this command, be it query or table.

  3. #3
    SteveF is offline Generally AccessAble
    Windows XP Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Agree with Ajetrumpet -- You need a database object to export, not code within a VBA module.

    However, that object doesn't need to be a table: a query will do just as well. A query in the Queries collection is actually SQL underneath, and if needed, you can use VBA to modify the SQL of a query prior to export:


    Dim qdf As DAO.QueryDef, dbs As DAO.Database

    Set dbs = CurrentDb
    Set qdf = dbs.QueryDefs("ExportQuery")
    qdf.SQL = "SELECT * FROM WorkTable;"
    qdf.Close

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "ExportQuery", "ExcelFileName", True


    Replace the items above with the actual SQL string you want to use (qdf.SQL), the name of the query and the full path to the Excel file.

    Steve

  4. #4
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    Quote Originally Posted by SteveF View Post
    Agree with Ajetrumpet -- You need a database object to export, not code within a VBA module.

    However, that object doesn't need to be a table: a query will do just as well. A query in the Queries collection is actually SQL underneath, and if needed, you can use VBA to modify the SQL of a query prior to export:


    Dim qdf As DAO.QueryDef, dbs As DAO.Database

    Set dbs = CurrentDb
    Set qdf = dbs.QueryDefs("ExportQuery")
    qdf.SQL = "SELECT * FROM WorkTable;"
    qdf.Close

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "ExportQuery", "ExcelFileName", True


    Replace the items above with the actual SQL string you want to use (qdf.SQL), the name of the query and the full path to the Excel file.

    Steve
    ok, this worked..

    So to clarify, I am initially defining qdf as an existing query in my database ("ExportQuery") and then redefining the SQL behind that query in code (i.e. maybe using different fields, adding criteria etc) using the underlying table of "ExportQuery"? (although it seems I could redefine it using any table? Not just the table underlying "ExportQuery"?)

    Thanks for your help!

  5. #5
    SteveF is offline Generally AccessAble
    Windows XP Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Quote Originally Posted by orcinus View Post
    ok, this worked..

    So to clarify, I am initially defining qdf as an existing query in my database ("ExportQuery") and then redefining the SQL behind that query in code (i.e. maybe using different fields, adding criteria etc) using the underlying table of "ExportQuery"? (although it seems I could redefine it using any table? Not just the table underlying "ExportQuery"?)

    Thanks for your help!
    The qdf variable in the code is your link to an existing query in the database. The Set statement creates the link to the specified query; after you Close the link you can Set it again to link to some other query if you like.

    All "queries" in Access actually use SQL statements to perform their actions -- depending on what they do one could regard them as the Access equivalent of either Views or Stored Procedures in other SQL RDBMSs. There's no reason you can't build a SQL statement in VBA that points to any table/tables in your database and stuff that into the SQL property of any query in your project's query collection. Needless to say, this capability needs to be used with caution: I'd probably have one or two queries in a project that were specifically reserved for this type of use, leaving the others alone to do what they were originally designed for.

    Steve

  6. #6
    orcinus is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    67
    Quote Originally Posted by SteveF View Post
    The qdf variable in the code is your link to an existing query in the database. The Set statement creates the link to the specified query; after you Close the link you can Set it again to link to some other query if you like.

    All "queries" in Access actually use SQL statements to perform their actions -- depending on what they do one could regard them as the Access equivalent of either Views or Stored Procedures in other SQL RDBMSs. There's no reason you can't build a SQL statement in VBA that points to any table/tables in your database and stuff that into the SQL property of any query in your project's query collection. Needless to say, this capability needs to be used with caution: I'd probably have one or two queries in a project that were specifically reserved for this type of use, leaving the others alone to do what they were originally designed for.

    Steve
    ok, thanks for the info..

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

Similar Threads

  1. Replies: 12
    Last Post: 09-13-2012, 12:07 PM
  2. Transferspreadsheet Help Please
    By graviz in forum Programming
    Replies: 0
    Last Post: 11-30-2009, 02:56 PM
  3. OpenReport Command with Double Check Where Statement
    By Robert M in forum Programming
    Replies: 3
    Last Post: 09-17-2009, 04:01 PM
  4. Replies: 0
    Last Post: 05-17-2008, 01:18 AM
  5. transferSpreadsheet
    By rabbit in forum Import/Export Data
    Replies: 1
    Last Post: 06-09-2006, 04:01 PM

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