G
Guest
I have an existing database but the entries are old. Can i clear the contents
and start again?
and start again?
jehova620 said:I have an existing database but the entries are old. Can i clear the contents
and start again?
Douglas J. Steele said:Vincent and Van have told you the mechanics of how to delete from existing
tables. I thought I'd point out that if you've set up relationships between
the tables to enforce Referential Integrity, you may have to futz around to
determine the correct sequence in which to delete the data. For example, if
you've got RI set up on a 1-to-many relationship between two tables, you
need to delete from the many side before you can delete from the 1 side.
Douglas J. Steele said:Vincent and Van have told you the mechanics of how to delete from existing
tables. I thought I'd point out that if you've set up relationships
between
the tables to enforce Referential Integrity, you may have to futz around
to
determine the correct sequence in which to delete the data. For example,
if
you've got RI set up on a 1-to-many relationship between two tables, you
need to delete from the many side before you can delete from the 1 side.
Douglas J. Steele said:Good idea, Arno. Of course, you might have a need something to expand
which tables you're excluding from deletion: sometimes you have tables of
legitimate values you want to keep, like countries or provinces/states.
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Douglas J. Steele said:Vincent and Van have told you the mechanics of how to delete from
existing
tables. I thought I'd point out that if you've set up relationships
between
the tables to enforce Referential Integrity, you may have to futz around
to
determine the correct sequence in which to delete the data. For example,
if
you've got RI set up on a 1-to-many relationship between two tables, you
need to delete from the many side before you can delete from the 1 side.
You could also delete all the tables in a loop and repeat the loop a few
(3-4) times.
Something like this code executed from the backend (no linked tables
here):
You can call this function in a macro if you like
Function DeleteDb ( )
On Error goto Err_DeleteDb
Dim db as Database, strSQL as string, i as integer, x as integer
Set db = CurrentDb
For i = 1 To 3 '3 or even 4 times may be needed because of RI
For x = 0 To db.Tabledefs.Count - 1
Select Case Left(db.Tabledefs(x).Name, 4)
Case "MSys" 'Don't delete the MSys* tables
Case Else
strSQL="DELETE * FROM [" & db.Tabledefs(x).Name & "]"
db.execute (strSQL)
End Select
Next x
Next i
Exit Function
Err_DeleteDb:
Msgbox Err & " " & Error$, vbCritical, "Error in function DeleteDb"
Exit Function
End Function
After this function you will need a compact.
Arno R
Douglas J. Steele said:Actually, rather than
Select Case Left(db.Tabledefs(x).Name, 4)
Case "MSys" 'Don't delete the MSys* tables
Case Else
strSQL="DELETE * FROM [" & db.Tabledefs(x).Name & "]"
db.execute (strSQL)
End Select
you might want to use
If (db.Tabledefs(x).Attributes And dbSystemObject) <> 0 Then
strSQL="DELETE * FROM [" & db.Tabledefs(x).Name & "]"
db.execute (strSQL)
End If
That way, you can easily add:
If (db.Tabledefs(x).Attributes And dbSystemObject) <> 0 Then
Select Case db.Tabledefs(x).Name
Case "Province", "Country"
' Do nothing
Case Else
strSQL="DELETE * FROM [" & db.Tabledefs(x).Name & "]"
db.execute (strSQL)
End Select
End If
Douglas J. Steele said:(BTW, why don't I get > signs on your responses when I try to respond?)