Solved

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

Posted on 2014-03-25
2
460 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
2 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now