Solved

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

Posted on 2009-05-08
6
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

630 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