Solved

Access vba copy to excel

Posted on 2014-11-25
9
154 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 85

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 85
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 85
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

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!

Question has a verified solution.

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

The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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