Results 1 to 2 of 2
  1. #1
    HarryScofs is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    16

    Exporting Selected Records from Access to Excel

    I need a way to export selected records in a Form to Excel. I found some code that I think will help me do this but I do not know how to attach it to a button. Here is the code:

    Public Function ExportToExcel(frmParent As Form, frmSubForm As Form)
    Dim rs As Recordset
    Dim intCount As Integer


    ' Create the Excel object
    Set mobjXl = New Excel.Application
    ' Fetch the recordset
    Set rs = frmParent.Form.Controls("frmSubForm.[Browse All Issues]").Form.RecordsetClone

    With mobjXl
    ' Add a workbook and turn of Excel updates
    .ScreenUpdating = True
    .Visible = False
    .Workbooks.Add
    .DisplayAlerts = True
    ' Add the column headers
    For intCount = 0 To rs.Fields.Count - 1
    .Cells(1, intCount + 1).Value = rs.Fields(intCount).Name
    Next intCount

    ' Dump the recordset to Excel
    .Range("A2").CopyFromRecordset rs
    .Visible = True
    End With
    End Function

    I am fiarly new to access so any help on how to do this best would be much appreciated.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I would make this a Sub instead of Function. The code could be in a general module or behind the form. Then in the button Click event select [Event Procedure] then click the ellipses (...) to go to the code editor. Type in code to call the export procedure. Or instead of making a call to the procedure, modify it to be placed right in the button Click.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Exporting from a Access Table to Excel PivotTable
    By runthis457 in forum Import/Export Data
    Replies: 2
    Last Post: 02-12-2011, 09:06 PM
  2. Replies: 1
    Last Post: 08-17-2010, 02:33 PM
  3. Exporting report selected from a listbox to excel
    By GARCHDEA in forum Import/Export Data
    Replies: 1
    Last Post: 08-10-2010, 07:45 AM
  4. Exporting Access queries to Excel
    By dbDamo in forum Import/Export Data
    Replies: 2
    Last Post: 09-22-2009, 01:42 AM
  5. Replies: 0
    Last Post: 06-05-2009, 10:47 AM

Tags for this Thread

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