Clearing contents of en excel sheet from a button in access form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ragnarok24
    New Member
    • Jul 2010
    • 7

    #1

    Clearing contents of en excel sheet from a button in access form

    Hi,

    I have a form in Access 2010 on which I would like to include a button which could clear an excelsheet save it and quit. I wrote the following code into the code builder of a command button.


    Code:
    Private Sub Command10_Click()
    'Need to declare outside of any Sub
    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    
     'open excel application
     Set xlApp = New Excel.Application
     'Open excel workbook
     Set xlWB = xlApp.Workbooks.Open(FileName:="C:\Users\user\Documents\Excel sheets\temptab.xlsm")
        'do your code here
    Sheet1.Cells.ClearContents
    ThisWorkbook.Save
    
     'Close workbook (optional)
    xlWB.Close
     'Quit excel (automatically closes all workbooks)
    xlApp.Quit
     'Clean up memory (you must do this)
     Set xlWB = Nothing
     Set xlApp = Nothing
    End Sub

    However, it shows Run-time error'424': Object required when I click on the button. When I open the Debugger it highlights Sheet1.Cells.Cl earContents

    Please help me with this
  • ragnarok24
    New Member
    • Jul 2010
    • 7

    #2
    Originally posted by ragnarok24
    Hi,

    I have a form in Access 2010 on which I would like to include a button which could clear an excelsheet save it and quit. I wrote the following code into the code builder of a command button.


    Code:
    Private Sub Command10_Click()
    'Need to declare outside of any Sub
    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    
     'open excel application
     Set xlApp = New Excel.Application
     'Open excel workbook
     Set xlWB = xlApp.Workbooks.Open(FileName:="C:\Users\user\Documents\Excel sheets\temptab.xlsm")
        'do your code here
    Sheet1.Cells.ClearContents
    ThisWorkbook.Save
    
     'Close workbook (optional)
    xlWB.Close
     'Quit excel (automatically closes all workbooks)
    xlApp.Quit
     'Clean up memory (you must do this)
     Set xlWB = Nothing
     Set xlApp = Nothing
    End Sub

    However, it shows Run-time error'424': Object required when I click on the button. When I open the Debugger it highlights Sheet1.Cells.Cl earContents

    Please help me with this
    I was searching for reasons for runtime error 424 and found that this problem may occur when the Data Access Object library is not properly referenced.
    Is there reference library I might be missing?

    Comment

    Working...