Solved

Export Access Table to a specific sheet in an existing Excel file

Posted on 2009-05-08
6
294 Views
Last Modified: 2013-11-27
I currently have code listed below in the form of a Microsoft Access application that copies an Excel Spreadsheet from a centralized template and pastes a new copy of it to a local location for the user to enter data on for a current incident, then save it or e-mail it without corrupting the original template.  A lot of the data requested in the Excel template is already in my Access DB and I have written a query (qry_Investigate) that asks the user what incident# they are working with and creates a local table (local_tbl_Investigate_Temp) isolating this information for them.  I would like to incorporate into my code the ability to export the information from this Access table into the second worksheet of the opened local Excel workbook (F:\FM Accident Invest Write-in.xls) from Sheet2!A1:Sheet2!P1 (16 data items), then have the template on Sheet1 of the workbook populate the associated fields so the end user does not have to re-type data already stored in the DB.

Please note: I use this code for a lot of forms that I pull from a central location.  So, I have a table (tbl_Path) that stores the locations of each template.
Private Sub Run_Report_Click()
 
Dim db As Database
Dim path_RST As Recordset
 
Dim path_STR As String
Dim image1_Src As String
Dim image1_Dst As String
Dim fname1 As String
Dim pfname1 As String
Dim msg As Integer
Dim msg2 As Integer
Dim fs
 
Set db = CurrentDb
Set path_RST = db.OpenRecordset("tbl_Path", dbOpenDynaset)
Set fs = CreateObject("Scripting.FileSystemObject")
 
image1_Src = path_RST![path]
image1_Dst = "F:\"
fs.CopyFile image1_Src, image1_Dst
fname1 = Right(path_RST![path], (Len(path_RST![path]) - 30))
pfname1 = image1_Dst & fname1
msg = MsgBox("The copy of this spreadsheet you are viewing has been saved to your (F:) drive and named: " & fname1, vbOKOnly, "Working Copy")
    
FollowHyperlink pfname1
End Sub

Open in new window

0
Comment
Question by:jmcclosk
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24338143
I recommend using the DoCmd.TransferSpreadsheet function.
DoCmd.TransferSpreadsheet, acSpreadsheetTypeExcel9, "TableName", "FilePathAndName", True ' HasFieldNames(Boolean)

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 24338177
this code will export tableX to specified name of sheet of  the excel file

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
      "tableX", "C:\myExcel.xls, True, "NameOfsheet"
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24338218
Sorry, I left out the acExport.
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TableName", "FilePathAndName", True

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Closing Comment

by:jmcclosk
ID: 31579557
It works great!  Thanks!
0
 

Author Comment

by:jmcclosk
ID: 24338405
Both of you had the same solution. cwood-wm-com posted it four minutes faster, but left out a needed part of the command (which was corrected).  Since it was so close time-wise, and you both had the same answer, I felt it was only right to give the points to capricorn1 because he got all of it on the first try. Sorry...
Thanks to both of you!
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24338415
Absolutely right. You are welcome.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question