Solved

Access vba copy to excel

Posted on 2014-11-25
9
134 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Normalization of a table 19 74
Top 1 of each supplier 55 57
help with an excel problem 10 30
Linking to an Excel file and determining the field types 3 16
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

809 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