MS Access 2003 - Embedded Excel Spreadsheet on Access form
Let's say I have an embedded Excel Spreadsheet on a Microsoft Access form. I call the object frame
and I add a text box on the form called
and I add a button on the form called
I want to type "Hello World" into the text box, click the button and have it appear in the A1 cell on that spreadsheet. What VBA do I use to accomplish this?
You can automate Excel, write your value to the worksheet, then update the object frame.
Private Sub cmdInsert_Click() Dim strPath As String Dim oExcel As Object Dim oSheet As Object Set oExcel = CreateObject("Excel.Application") oExcel.Visible = True strPath = Me.ExcelFrame.SourceDoc 'Debug.Print strPath ' oExcel.Workbooks.Open strPath Set oSheet = oExcel.ActiveSheet 'Debug.Print oSheet.Name ' oSheet.Range("A1").Value = Me.txtA1 oExcel.ActiveWorkbook.Save oExcel.Quit Set oSheet = Nothing Set oExcel = Nothing 'acOLEUpdate action requires Enabled = True ' 'and Locked = False ' Me.ExcelFrame.Enabled = True Me.ExcelFrame.Locked = False Me.ExcelFrame.Action = acOLEUpdate Me.txtA1.SetFocus Me.ExcelFrame.Enabled = False Me.ExcelFrame.Locked = True End Sub
Edit: The example was based on an external workbook file which is linked as the source for the form's object frame.
To link a worksheet, choose the "Create from File" radio button, check the "Link" check box, and browse to select the workbook. That's the way I did it with Access 2007. As I recall, it was similar with Access 2003.
It's never too late, right ?
Provide that you have already created the Excel object (as in OP):
Dim wb As Excel.Workbook, ws As Excel.Worksheet Set wb = Me.ExcelFrame.Object Set ws = wb.Worksheets(1) ws.range("a1")= "Hello world"
Note that this code requires a reference to MS Excel in VBA.