Solved

Access vba copy to excel

Posted on 2014-11-25
9
120 Views
Last Modified: 2014-12-16
I need an access vba code to copy a simple table from access into a shared excel workbook. I need access to overwrite any data in the target destination also. Lets say my table is called MyTable, and i need to copy it into MyWorkbook.xlsx on Sheet1. Please note, i had such a code, but access did not resave excel in shared mode.
0
Comment
Question by:jtencha
  • 3
  • 3
9 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
Comment Utility
You can just Delete the Workbook, and then use TransferSpreadsheet to move it:

If Dir("Your path to the MyWorkbook.xlsx file", "") <> "" Then
  Kill "Your path to the MyWorkbook.xlsx file"
End If

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table or Query Name", "Your Destination Path"

However, if someone has that file open, you can't do it. You'd have to wait until it's free.
0
 

Author Comment

by:jtencha
Comment Utility
Ok, will give that a try. Thanks
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 250 total points
Comment Utility
If you need to paste the table's data to a specific location in a workbook, you can use the CopyFromRecordset method, like this:

On Error Resume Next
               Set appExcel = GetObject(, "Excel.Application")
               Set wkbTest = appExcel.Workbooks(strXLFileName)
               
               If Err.Number = 0 Then
                  'Workbook is already open
                  wkbTest.Close savechanges:=xlDoNotSaveChanges
                  Set wkbTest = Nothing
               End If
               
               Set wkb = appExcel.Workbooks.Open(FileName:=strXLFile, _
                  ReadOnly:=False)
               appExcel.Visible = True
               wkb.Activate
               
On Error GoTo ErrorHandler
               Set sht = wkb.Sheets("Access Data")
               
               'Clear old data, if any
               lngLastRow = sht.UsedRange.Rows.Count + 2
               strRange = "A3:Q" & CStr(lngLastRow)
               Set rng = sht.Range(strRange)
               rng.ClearContents
                              
               'Create a DAO recordset
               Set rng = sht.Range("A3")
               Set rstDAO = CurrentDb.OpenRecordset(strRecordSource)
               rng.CopyFromRecordset rstDAO
                                       
               strTitle = "Export successful"
               strPrompt = strXLFile & " filled with current Access data"
               MsgBox prompt:=strPrompt, _
                  Buttons:=vbInformation + vbOKOnly, _
                  Title:=strTitle

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jtencha
Comment Utility
thanks for the help!
0
 

Author Comment

by:jtencha
Comment Utility
I've requested that this question be closed as follows:

Accepted answer: 0 points for jtencha's comment #a40485048

for the following reason:

it worked
0
 
LVL 84
Comment Utility
I think you may have chosen the wrong comment when selecting your answer. You chose http:#a40485048, which is your last comment.
0
 
LVL 84
Comment Utility
Both mine and Helen's answer should be accepted:

http:#a40465129
http:#a40472483

Each provides details of how to delete the file, and how to transfer the file
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Viewers will learn a basic relationship technique in Power Pivot for Excel 2013.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now