Results 1 to 5 of 5
  1. #1
    JC-Access is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    4

    Try to export queries to excel worksheets; Error '3191' cannot define field more than once.

    I am trying to export multiple access queries to a single excel workbook as individual worksheets. It creates the workbook and sheet, runs the query, but isn't placing the data into the worksheet.

    Any help is greatly appreciated.



    When I run the module it errors out on this line, with the error "run-time error: 3191" Cannot define field more than once.

    Code:
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "mySheet1", xlsxPath, True
    Code:
    Sub ExportToXlsx()
        Dim cdb As DAO.Database, qdf As DAO.QueryDef
        Set cdb = CurrentDb
    
        Const xlsxPath = "d:\foo.xlsx"
    
        ' create .xlsx file if it doesn't already exist, and add the first worksheet
        Set qdf = cdb.CreateQueryDef("mySheet1", _
                "SELECT [Backup Properties].*, [Backup Properties].Application FROM [Backup Properties]WHERE (([Backup Properties].Application)='Remedy CSD Servers')")
        Set qdf = Nothing
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "mySheet1", xlsxPath, True
        DoCmd.DeleteObject acQuery, "mySheet1"
    
        ' file exists now, so this will add a second worksheet to the file
        Set qdf = cdb.CreateQueryDef("mySheet2", _
                "SELECT * FROM Clients WHERE ID Between 6 And 10")
        Set qdf = Nothing
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "mySheet2", xlsxPath, True
        DoCmd.DeleteObject acQuery, "mySheet2"
    
        Set cdb = Nothing
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qsQuery", "c:\file.xls", True, "sheet1"

  3. #3
    JC-Access is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    4
    Thank you for your reply.

    I replaced that line and it isn't working. Does that line replace a larger section of the script?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qsQuery1", "c:\file.xls", True, "sheet1"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "qsQuery2", "c:\file.xls", True, "sheet2"

    you dont need
    DoCmd.DeleteObject acQuery, "mySheet2"

    if it still doesnt work, then you are missing something, either a query mySheet1, or change the export file type to :acSpreadsheetTypeExcel12


  5. #5
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    your issue is here

    ....SELECT [Backup Properties].*, [Backup Properties].Application FROM...

    Your error is Error '3191' cannot define field more than once - and you defining it twice, once in the .* and once specifically. Remove the bit highlighted in red

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

Similar Threads

  1. Replies: 6
    Last Post: 01-29-2015, 08:53 PM
  2. Export Queries to Excel with Combo-Box visible in Excel
    By johnmarc2 in forum Import/Export Data
    Replies: 1
    Last Post: 07-07-2014, 05:33 PM
  3. Replies: 12
    Last Post: 04-15-2014, 12:16 PM
  4. Replies: 3
    Last Post: 03-16-2014, 08:09 PM
  5. Replies: 1
    Last Post: 03-12-2012, 02:21 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