Una famiglia di sistemi di gestione per database relazionali di Microsoft progettati per semplificare l'uso.
ciao Nicola,
mi perplime il fatto che voglia cancellare dati dal database in tal modo...
In ogni caso...se I database sono solo 3 potresti provare come segue, personalizzando il nome dei tre databases come indicato qui sotto.
La routine va inserita in un modulo standard a lanciata dalla sub kickoff() dal tuo DB da cui vuoi effettuare l'operazione.
Alla fine ottieni tre database clone dei tuoi, completamente vuoti e compattati.
Ad ogni esecuzione non hai la sovrascrittura dei precedenti perche' la modalita' di concatenazione circa la costruzione del nome ne garantisce l'unicita.
La routine ri-crea anche le relazione nei db nuovi.
Prova :
Option Compare Database
Option Explicit
Private objDbSource As Object ' Access.Application
Private newCurrentDb As DAO.Database
Public Sub kickoff()
Dim aDataBase(2) As String
Dim i As Long
aDataBase(0) = "C:\Northwind2007_3.accdb" ' personalizza path e nome del DB
aDataBase(1) = "C:\Northwind2007_4.accdb" ' personalizza path e nome del DB
aDataBase(2) = "C:\Northwind2007_5.accdb" ' personalizza path e nome del DB
Set objDbSource = CreateObject("Access.Application")
For i = 0 To UBound(aDataBase())
objDbSource.OpenCurrentDatabase filePAth:=aDataBase(i)
Set newCurrentDb = objDbSource.DBEngine(0)(aDataBase(i))
myNewdB strDataBaseFullPath:=aDataBase(i)
Next
exit_here:
Erase aDataBase()
Set objDbSource = Nothing
Set newCurrentDb = Nothing
VBA.MsgBox prompt:="Ho finito.", _
buttons:=vbInformation, _
title:="Informazione"
Exit Sub
errHandler:
With Err
VBA.MsgBox "ERR#" & .Number _
& vbNewLine & .Description _
, vbOKOnly Or vbCritical
Resume exit_here
End With
End Sub
Private Sub myNewdB(ByVal strDataBaseFullPath As String)
On Error GoTo errHandler
Dim strRand As String
strRand = "_new_" & CStr(CLng(Rnd() * 10000 * CDbl(VBA.Now))) & ".accdb"
Dim strNewDB As String
strNewDB = GetFilePath(strPath:=strDataBaseFullPath) & GetFileText(strPath:=strDataBaseFullPath) & strRand
If FileExists(strNewDB) Then Exit Sub
With DBEngine
.Idle
Dim newDb As DAO.Database
Set newDb = .CreateDatabase(Name:=strNewDB, _
locale:=dbLangGeneral, _
option:=dbVersion120)
newDb.Close
copyTables strDBNewName:=strNewDB, strOldDbName:=strDataBaseFullPath
copyRel strDBName:=strNewDB
.CompactDatabase strNewDB, strNewDB & 2
Kill strNewDB & 2
End With
exit_here:
Set newDb = Nothing
Exit Sub
errHandler:
With Err
VBA.MsgBox "ERR#" & .Number _
& vbNewLine & .Description _
, vbOKOnly Or vbCritical
Resume exit_here
End With
End Sub
Private Sub copyTables(ByVal strDBNewName As String, ByVal strOldDbName As String)
On Error GoTo errHandler
Dim tdf As DAO.TableDef
For Each tdf In newCurrentDb.TableDefs
If Not DAO.dbSystemObject And tdf.Attributes = 0 Then
objDbSource.DoCmd.CopyObject DestinationDatabase:=strDBNewName, _
SourceObjectType:=acTable, _
SourceObjectName:=tdf.Name
newCurrentDb.Execute Query:="delete * from [" & tdf.Name & "];"
End If
Next
exit_here:
Set tdf = Nothing
Exit Sub
errHandler:
With Err
VBA.MsgBox "ERR#" & .Number _
& vbNewLine & .Description _
, vbOKOnly Or vbCritical
Resume exit_here
End With
End Sub
Private Sub copyRel(ByVal strDBName As String)
On Error GoTo errHandler
Dim OrRel As DAO.Relation
Dim DeRel As DAO.Relation
Dim dbsBK As DAO.Database
Set dbsBK = OpenDatabase(strDBName, False, False)
For Each OrRel In newCurrentDb.Relations
' If OrRel.Attributes = 0 Then
If Left$(OrRel.Name, 4) <> "msys" Then
'Debug.Print OrRel.ForeignTable
Set DeRel = dbsBK.CreateRelation(Name:="new" & OrRel.Name, _
Table:=OrRel.Table, _
ForeignTable:=OrRel.ForeignTable, _
Attributes:=OrRel.Attributes)
DeRel.Fields.Append DeRel.CreateField(OrRel.Fields(0).Name)
DeRel.Fields(DeRel.Fields(0).Name).ForeignName = OrRel.Fields(0).ForeignName
dbsBK.Relations.Append DeRel
End If
Next
Set OrRel = Nothing
Set DeRel = Nothing
Set dbsBK = Nothing
objDbSource.CloseCurrentDatabase
newCurrentDb.Close
exit_here:
Set OrRel = Nothing
Set DeRel = Nothing
Set dbsBK = Nothing
Exit Sub
errHandler:
With Err
VBA.MsgBox "ERR#" & .Number _
& vbNewLine & .Description _
, vbOKOnly Or vbCritical
Resume exit_here
End With
End Sub
Private Function FileExists(strPathFile As String) As Boolean
On Error Resume Next
FileExists = ((GetAttr(strPathFile) And vbDirectory) = 0)
End Function
Public Function GetFilePath(strPath As String) As String
Dim strIn As String
strIn = Mid$(strPath, 1, InStrRev(strPath, ""))
GetFilePath = strIn
End Function
Public Function GetFileName(strPath As String) As String
Dim strIn As String
strIn = Mid$(strPath, InStrRev(strPath, "") + 1, 255)
GetFileName = strIn
End Function
Public Function GetFileText(strPath As String) As String
Dim strIn As String
Dim intStart As Integer
Dim intStop As Integer
intStart = InStrRev(strPath, "") + 1
intStop = InStrRev(strPath, ".")
strIn = Mid$(strPath, intStart, intStop - intStart)
strIn = Replace(strIn, "'", "''")
strIn = Replace(strIn, ".", "")
GetFileText = strIn
End Function
HTH.
Ciao, Sandro