Solved

Access vba copy to excel

Posted on 2014-11-25
9
143 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
ID: 40465129
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
ID: 40465872
Ok, will give that a try. Thanks
0
 
LVL 31

Assisted Solution

by:Helen_Feddema
Helen_Feddema earned 250 total points
ID: 40472483
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
Independent Software Vendors: 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 Comment

by:jtencha
ID: 40485048
thanks for the help!
0
 

Author Comment

by:jtencha
ID: 40485483
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
ID: 40485484
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
ID: 40495926
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Report Expression 4 26
Format as currecy for just the one current record 2 21
MS Access, hourly employee status 8 42
How do a DCount on a report 1 17
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.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

756 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