views:

388

answers:

1

Let's say I have an embedded Excel Spreadsheet on a Microsoft Access form. I call the object frame

ExcelFrame

and I add a text box on the form called

txtA1

and I add a button on the form called

cmdInsert

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?

Thanks

+1  A: 

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.

HansUp
@Hans....everytime i try it, i get a runtime error telling me "could not be found. Check the spelling of the file name, and make sure...." But my objects on the form are named exactly what are in the above code/example. It seem to stop whenever it tries to open the excel frame, and then nothing. You have to click back to the vba tab to see the error. thanks hans
Justin
@Justin "spelling of the file name ..." may mean it can't find the workbook file. Check the value of strPath by uncommenting Debug.Print strPath. If strPath includes spaces, you'll have to figure out how to feed it to Workbooks.Open as a single argument. I would just move the workbook to a folder whose name doesn't include spaces.
HansUp
@Hans...oooohh i see. I did not even use a pre saved excel file on the form. I just used the > INSERT > OBJECT > EXCEL SPREADSHEET to place the spreadsheet on the form. So the above method means using an independant saved excel file, and then when I want to add it to a form I have to INSERT ? CREATEFROMFILE > select it?
Justin
@Hans...I saved an excel file onto my desktop, then inserted it into the form. Still get the same error, but it is because strPath is not getting passed anything....when I step through it says strPath = ""
Justin
@Justin I added a note that my method was based on a **linked** worksheet. Sorry for not making that point clear at the outset. I don't see how to edit a static embedded copy of a worksheet.
HansUp
@Hans....ah I see. How can I add a spreadsheet to a form that is linked instead of embedded? Can I? Thanks anyway Hans!
Justin
@Justin I edited the answer to describe linking a worksheet with A2007. If that doesn't work for you, let me know and I'll start up another machine with A2003.
HansUp
Thanks Hans that worked just fine! I was trying to make an interactive map scheme because I have a bunch of shape range objects in the excel file, with events, etc. So i wanted the excel file to be embedded that way you could interact with it. If I link it then I can't have it this way. But at least I learned something new.....the above works fine now! Thanks again!
Justin
@Justin You can interact with it if you set the object frame properties Enabled = True and Locked = False. Then double-clicking the linked worksheet object will open it in Excel. Unless you had something else in mind for "interact" ...
HansUp
I will give it a try.....thanks for all your help!! As always you have helped me out and I appreciate it Hans!
Justin