Solved

Open specific Excel workbook and write to it from Access 2010 database

Posted on 2014-03-25
2
474 Views
Last Modified: 2014-03-25
I want to open a specific Excel workbook from an Access 2010 database and write data to it. I have tried, but do not succeed.
The Access 2010 database has a form named "Form1" with a textbox  "txtWorkbookToOpen".
I enter a path to an Excel workbook in the textbox  "C:\AccessExcel\tblSales.xlsx" .
Then I want to click on a button "cmdOpenExcelWorkbook" to open that workbook and write some data to it. Say I want to write 100 in cell A1 of the active sheet.
Please help with the VBA.
I attach the database and the Excel workbook in case that will help.
Database14.accdb
tblSales.xlsx
0
Comment
Question by:Fritz Paul
[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
2 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 39954923
Just make a reference to the Microsoft Excel Object Library in your project and then use something like

Dim excel As Excel.Application
Dim wb As Excel.Workbook

Set excel = CreateObject("Excel.Application")
Set wb = excel.Workbooks.Open(txtWorkbookToOpen.Text)
excel.Visible = True
wb.ActiveSheet.Range("A1").Value = 100
excel.Quit
Set excel = Nothing
Set wb = Nothing

Open in new window


For some more info have a look at http://support.microsoft.com/kb/198571

Michael
0
 

Author Closing Comment

by:Fritz Paul
ID: 39955201
Thanks,

I got a error message:

Run-time error '2185':
You can't reference a property or method for a control unless the control has the focus.

I then changed the code as below and it works. (However Excel still shows in my Windows Task Manager.)

Dim excel As excel.Application
Dim wb As excel.Workbook
Dim WorkbookToOpen As String

WorkbookToOpen = Me.txtWorkbookToOpen
Debug.Print WorkbookToOpen
Set excel = CreateObject("Excel.Application")
Set wb = excel.Workbooks.Open(WorkbookToOpen)
'Set wb = excel.Workbooks.Open("C:\AccessExcel\tblSales.xlsx")
excel.Visible = True
wb.ActiveSheet.Range("A1").Value = 100
excel.Quit
Set excel = Nothing
Set wb = Nothing
0

Featured Post

Technology Partners: 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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

734 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