Results 1 to 3 of 3
  1. #1
    bbylls is offline Advanced Beginner
    Windows Vista Access 2003
    Join Date
    Dec 2009
    Posts
    33

    Button on form to delete all records

    I am looking for code I can put on a button in a form to export all data in a single table to an Excel file, and then delete all of the records in the table.
    I would also like to then have a button to import records into the table from an Excel spreadsheet.
    Sounds crazy, but every year I have to start with fresh data in the table.
    If I can export and import, at least the data will never be 'LOST'.


    Thank you.

  2. #2
    SoftwareMatters is offline Access VBA Developers
    Windows XP Access 2003
    Join Date
    Mar 2009
    Location
    Dorset
    Posts
    274
    To export all data to Excel use this:
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "TableName", "Path"

    To delete all the records in the table write a delete query and have it trigger from the button like this:
    DoCmd.OpenQuery "DeleteQueryName", acNormal, acEdit

    To import all the records from Excel you need to link the Excel file as a table then werite an append query and trigger that from the button like this:
    DoCmd.OpenQuery "AppendQueryName", acNormal, acEdit

    Note: If you are going to trigger queries using this method it is a good idea to turn the warnings off at the start and then back on at the end to stop annoying messages:

    DoCmd.SetWarnings False

    code here...

    DoCmd.SetWarnings True

  3. #3
    bbylls is offline Advanced Beginner
    Windows Vista Access 2003
    Join Date
    Dec 2009
    Posts
    33
    I did this:
    Private Sub ExportData_Click()
    On Error GoTo Err_ExportData_Click

    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", "C:\Test.xls"

    Exit_ExportData_Click:
    Exit Sub

    Err_ExportData_Click:
    MsgBox Err.Description
    Resume Exit_ExportData_Click

    End Sub

    And got this error message:
    The Microsoft Jet database engine cannot open the file 'C:\Test.xls'. It is already opened exclusively by another user, or you need permission to view its data.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Simple delete button on form
    By chessico in forum Forms
    Replies: 9
    Last Post: 10-15-2009, 03:14 PM
  2. Error while trying to add delete link button.
    By islandboy in forum Access
    Replies: 3
    Last Post: 09-03-2009, 09:32 AM
  3. Command button help - delete record
    By Zukster in forum Forms
    Replies: 0
    Last Post: 08-27-2009, 08:47 AM
  4. #delete records
    By supernova122 in forum Access
    Replies: 0
    Last Post: 07-08-2009, 08:41 AM
  5. Add/Delete Button
    By ocemy in forum Forms
    Replies: 1
    Last Post: 03-29-2009, 08:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums