Solved

Access vba copy to excel

Posted on 2014-11-25
9
148 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
[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
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Viewers will learn the basics of the new Quick Analysis feature in Excel 2013.
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 …

752 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