0

I need help coming up with a method to allow a user to export a query's results to an xls file on a button click event. I've tried using an Output To macro, but it doesn't work for a query containing 30,000+ records.

Thanks in advance

3
  • Can you post your code? (On my answer you indicated you tried the docmd.) Commented Sep 18, 2009 at 21:03
  • What's the destination version of Excel? The limitation on numer of rows is unquestionably an Excel limit, not an Access one. Excel 2007 ups the number of rows, BTW. Commented Sep 20, 2009 at 2:52
  • 1
    Actually, Tony, I think the Access 2002 tag is useful information here, as it limits the export formats. Of course, the way I think tags should be used is that only MS-ACCESS should be used and the specific version indicated in the question, but you removed the tag but didn't add the version into the question text. That loses useful information, in my opinion. Commented Sep 11, 2010 at 19:41

2 Answers 2

4

You might want to consider using Automation to create an Excel spreadsheet and populate it on your own rather than using a macro.

Here's a function I have used in the past to do just that.

Public Function ExportToExcel(FileToCreate As String, ByRef rst As ADODB.Recordset)
'Parms:     FileToCreate - Full path and file name to Excel spreadsheet to create
'           rst - Populated ADO recordset to export

On Error GoTo Err_Handler

Dim objExcel As Object
Dim objBook As Object
Dim objSheet As Object

'Create a new excel workbook; use late binding to prevent issues with different versions of Excel being
'installed on dev machine vs user machine
Set objExcel = CreateObject("Excel.Application")
Set objBook = objExcel.Workbooks.Add

'Hide the workbook temporarily from the user
objExcel.Visible = False

objBook.SaveAs (FileToCreate)

'Remove Worksheets so we're left with just one in the Workbook for starters
Do Until objBook.Worksheets.Count = 1
   Set objSheet = objBook.Worksheets(objBook.Worksheets.Count - 1)
   objSheet.Delete
Loop

Set objSheet = objBook.Worksheets(1)

rst.MoveFirst

'Use CopyFromRecordset method as this is faster than writing data one row at a time
objSheet.Range("A1").CopyFromRecordset rst

'The UsedRange.Rows.Count property can be used to identify the last row of actual data in the spreadsheet
'This is sometimes useful if you need to add a summary row or otherwise manipulate the data
'Dim lngUsedRange As Long
'lngUsedRange = objSheet.UsedRange.Rows.Count

'Save the spreadsheet
objBook.Save

objExcel.Visible = True

ExportToExcel = True

Err_Handler:
   Set objSheet = Nothing
   Set objBook = Nothing
   Set objExcel = Nothing
   DoCmd.Hourglass False

   If Err.Number <> 0 Then
      Err.Raise Err.Number, Err.Source, Err.Description
   End If
End Function
Sign up to request clarification or add additional context in comments.

1 Comment

this code works for me well, but the only problem is that the excel data has no headers.. how can i add it?
1

Can you use VBA?

Intellisense will help you, but get started with:

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "my_query_name", "C:\myfilename.xls"

Note: you may have a different Excel version "my_query_name" is the name of your query or table you'll need to set the file location to the appropriate location\name .extension

More Info: http://msdn.microsoft.com/en-us/library/bb214134.aspx

1 Comment

i've tried that but i keep getting a file does not exist in that location error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.