Archiving records in multiple tables

rastus

Board Regular
Joined
Jul 8, 2005
Messages
157
Hi

My company has an access database that has records with details recorded in several tables. For example, the main table has personal account details. The second table has dates of customer purchase linked to the main table by account number.

How do I delete an account number record so that all references to it in other tables are also deleted?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Why delete records?

Why not just add an extra field that indicates whether the account is active or not?

This could be used in queries to only return records for active accounts.
 
Upvote 0
How big is it?

How is the data structured?
 
Upvote 0
If deleting is your only way. (Norie Sugestion is the standard, you will lose your data for ever if you delete) You need to start deleting by the children.

Let say you have a table call customer, an other call invoice and the last one call invoicedetail. You need to start deleting invoicedetail first, than delete the invoice data, than finaly delete customer data. (the invoicedata is link to the invoice, wich is link to a customer in this case) If I explain well

But first try to index your table to speed it up..

Good Luck,
 
Upvote 0
I think he's looking for a long term plan. No matter how much you index, eventually you're going to need to move data out of the active tables.

Take a look at Cascade updates/deletions. It's a relationship property that essentially allows you to delete the Parent record and it cascades down to all child records in other tables.

One technique suggestion is this:
Really this is a clip out of some other functions and I've attempted to remove some irrelevant items. Some of the optional items might be helpful to you.

Mostly I use this code to transfer information to a remote archive location. The code extracts all the field names in source/destination tables and creates a SQL MakeTable or Append query to do the work.

Mike

Code:
Call Assembler("Make", desttblname, srctbl, , , , exportPath)
Call Assembler("Append", desttblname, srctbl, , , , exportPath)

Public Function Assembler(ByVal strMode As String, ByVal tblName As String, _
                        Optional ByVal tblSource As String, _
                        Optional ByVal strFld As String, Optional strParam As String, _
                        Optional strType As String, _
                        Optional remoteDatabase As String, _
                        Optional importPath As String)
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim intNum As Integer

On Error GoTo HandleErr
Set dbs = CurrentDb()

If InStr(tblName, "[") > 0 Then
   tblName = Mid(tblName, 2, Len(tblName) - 2)
End If
If InStr(tblSource, "[") > 0 Then
   tblSource = Mid(tblSource, 2, Len(tblSource) - 2)
End If
If Len(remoteDatabase) > 0 Then
  tblName = "[" & remoteDatabase & "]." & tblName
End If

If Len(importPath) > 0 Then
  tblSource = "[" & importPath & "]." & tblSource
End If

If IsNumeric(strFld) Then intNum = strFld
' this is where the error is - intNum isn't getting a valid numeric value

Select Case strMode
  Case "delete"
    strSQL = "DELETE * FROM " & tblName '& "]"
  Case "append"
    strSQL = "INSERT INTO " & tblName & " ( " & GetFlds(tblName) & " ) "

    strSQL = strSQL & "SELECT " & GetFlds(tblSource, strFld, intNum) & " FROM [" _
               & tblSource & "]"
  Case "make"
    strSQL = "SELECT " & GetFlds(tblSource) & " INTO " & tblName & " FROM " & tblSource
  Case Else:
End Select
If Len(strParam) > 0 Then
  If InStr(strParam, "Like") Then
   strSQL = strSQL & " WHERE " & strParam
  Else
    strSQL = strSQL & " WHERE " & strFld & " = '" & strParam & "'"
  End If
End If
DoCmd.RunSQL strSQL

ExitHere:
Set rs = Nothing
Set dbs = Nothing
End Function

Public Function GetFlds(ByVal MyTable As String, _
                        Optional HowMany As Integer) As String
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim x, intFields As Integer

On Error GoTo HandleErr
Set dbs = CurrentDb()
If InStr(MyTable, "[") = 0 Then
   strSQL = "SELECT * FROM [" & MyTable & "]"
Else
   strSQL = "SELECT * FROM " & MyTable
End If

Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)

With rs
  ' If I'm inserting an additional field at a given position
  ' then add one to the number of fields now
  intFields = .Fields.Count - 1
  For x = 0 To intFields
    If HowMany > 0 And x > HowMany Then Exit For   ' Sets limit on how many fields actually used
    If x = myType Then
       GetFlds = GetFlds & "' ', "
    End If
        GetFlds = GetFlds & "[" & .Fields(x).Name & "], "
  Next x
  If HowMany > intFields + 1 Then  ' if need more blank fields, add
    For x = x To HowMany
       GetFlds = GetFlds & "' ', "
    Next x
  End If
End With

GetFlds = Trim(Left(GetFlds, Len(GetFlds) - 2))

End Function
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top