Results 1 to 5 of 5
  1. #1
    tlyons is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    10

    Delete button with archive Table

    Hey, I need some expert advice. I'm trying to create a delete button in a form which would delete a record from the current table and transfer it to a seperate archive table. Any suggestions would be much appreciated. Tks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Generally speaking you shouldn't move records around. I'd have a "status" field that you would simply mark as archived. That leaves all records in one table, where you can easily query current, archived or all records. If you really want to do it, you'd run an append query and then a delete query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I'm with Pbaldy on this one.

    Unless you're dealing with performance or size issues with your database, I don't recommend deleting records from your Table. It's far better (and easier) to just add an "Archived" Yes/No field. For the Records you need to delete, just mark the field Yes.

    If you absolutely have to (and, unfortunately, having your boss say "because I said so" is a valid reason :/), then you'd make a VBA function that does the following:

    1. Opens both your "main" Table and the "archive" Table.
    2. Copies the Record over to your "archive" Table.
    3. Deletes the Record from your "main" Table.

    If you're not familiar with VBA coding and have to go this route, then just let us know.

  4. #4
    Nixx1401 is offline Competent Performer
    Windows Vista Access 2003
    Join Date
    Feb 2010
    Location
    Barbados
    Posts
    115
    can you post the relative VBA code for the above solution

    Nixx1401

  5. #5
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    I'm assuming you're asking for VBA code for the second option, since the first is extremely simple (just add 1 field to the Table and then Filter all your Queries where that field = True).

    Code:
      On Error GoTo Error_Archive
    
      Dim work As Workspace
      Dim rstTable1 As DAO.Recordset
      Dim rstTable2 As DAO.Recordset
    
      Dim boolTransActive As Boolean
      Dim strCriteria As String
    
      boolTransActive = False
    
      Set work = DBEngine(0)
    
      ' Assumes Me!RecordID is the Record from Table1 that you want to archive in Table2.
      Set rstTable1 = CurrentDb().OpenRecordset("SELECT * FROM Table1 WHERE [ID_Field]=" & Me!RecordID, dbOpenDynaset)
      Set rstTable2 = CurrentDb().OpenRecordset("SELECT * FROM Table2 WHERE [ID_Field]=" & Me!RecordID, dbOpenDynaset)
    
      If rstTable2.RecordCount > 0 Then
        MsgBox "Error! This Record has already been archived! You will need to manually remove it from Table1."
        GoTo FunctionClosing
      End If
    
      work.BeginTrans
      boolTransActive = True
    
      ' Add the Record to the archival Table.
      With rstTable2
        .AddNew
        !Field1 = rstTable1("Field1")
        !Field2 = rstTable1("Field2")
        !Field3 = rstTable1("Field3")
        .Update
      End With
    
      ' Delete the Record from the original Table.
      With rstTable1
        .Delete
        .Update
      End With
    
      work.CommitTrans
      boolTransActive = False
    
    :FunctionClosing:
      rstTable1.Close
      rstTable2.Close
    
      Set work = Nothing
      Set rstTable1 = Nothing
      Set rstTable2 = Nothing
    
      Exit Function
    
    Error_Archive:
      If boolTransActive = True Then
        work.Rollback
      End If
    
      MsgBox "The following error occurred while attempting to archive Record " & CStr(Me!RecordID) & ", please contact your System Administrator." & _
             vbCrLf & vbCrLf & "'" & Err.Description & "'"
    
      Resume FunctionClosing
    You'll need to replace the bolded text with whatever's appropriate for your Table setup. Also, this is aircode, so there may be a few minor typos/etc. in it.
    Last edited by Rawb; 07-08-2010 at 12:19 PM. Reason: Clarification!

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

Similar Threads

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

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