Results 1 to 6 of 6
  1. #1
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283

    DoCmd.TransferSpreadsheet acExport Question

    Hello,

    I was wondering if it's possible to export a query without saving it as an object in the front-end. Write out the query in the module and export it? Here below is my code: Let me know is something is wrong. Thanks

    Public Sub Test()
    Dim Str As String
    Dim fd As FileDialog

    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    Str = "SELECT Hello.test FROM Table;"

    With fd
    .AllowMultiSelect = False
    .Title = "Save File"
    .InitialFileName = "Str.xlsx"

    If .Show = True Then
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Str", .SelectedItems(1)

    Else

    MsgBox "No File was selected"

    End If

    End With
    Set fd = Nothing



    End Sub

  2. #2
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Sadly no. Your options are to export a table or a query, or you can open Excel and copy/paste. You can turn your "Str" into a make-table query, run it and export the table or use the Str to create a query.

  3. #3
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    That's lame. But thank you. I just hate having so many objects.

  4. #4
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Delete them when you are done.

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I was wondering if it's possible to export a query without saving it as an object in the front-end
    I would say it is not possible.

    Reasoning:
    One of the parameters of the DoCmd.TransferSpreadsheet method is a string that is the name of a table or query.
    If you have a table named "Table1", access knows it should open the table to get the data.
    If you have a saved query named "Query1", access knows it should open the execute the query to get the data.

    Lets say you have a variable "Var1" that is declared as a string. The value of "Var1" is "SELECT Hello.test FROM Table;".
    If you use "Var1" (with quotes) as the value for the parameter TableName, Access looks for a Table/Query named "Var1". Doesn't execute because "Var1" doesn't exist.
    What happens if you replace "Var1" with the SQL string "SELECT Hello.test FROM Table;"?? Still doesn't execute because Access doesn't know how to handle the SQL because it is looking for the name of a Table/Query.

    If you use Var1 (without quotes), and variable Var1 value is "Table1", the command will execute because Access knows that Var1 is a variable, so it reads the value Var1 (=Table1), finds that "Table1" exists and the command completes.

    If you change the value of Var1 to be the SQL string "SELECT Hello.test FROM Table;" what happens?
    Access will try to find a table or query named "SELECT Hello.test FROM Table;". There is no table/query named "SELECT Hello.test FROM Table;" so the command fails.


    I don't see any possibility of using a SQL string in place of a table or query name.......

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    You can automate Excel to copy a recordset that exists in Access. For instance, you can create a DAO recordset using an SQL statement or clone a Form's recordset. Then, you can copy that recordset over to your Worksheet. Here is a snippet.
    https://www.accessforums.net/showthr...494#post281494

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

Similar Threads

  1. Replies: 12
    Last Post: 05-31-2016, 02:12 PM
  2. How do I use DoCmd.TransferSpreadsheet acExport
    By morerockin in forum Access
    Replies: 6
    Last Post: 09-24-2015, 03:28 PM
  3. Replies: 8
    Last Post: 07-02-2014, 10:58 AM
  4. Importing Excel file: ADODB, DAO or DoCmd.TransferSpreadsheet
    By Monterey_Manzer in forum Import/Export Data
    Replies: 3
    Last Post: 08-13-2013, 11:34 AM
  5. Replies: 4
    Last Post: 11-09-2011, 08:40 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