G
Greg J
Here is a function I have created that enables you to compact or copy a
linked database to a new database. I need to copy the linked database
to a website so I need to be able to create a compacted copy of the
database. As it was linked, it wouldnt let me do it from the front
end. As a result, I have come up with the following routine that does
the following:
1. Gets a list of all the tables that are currently linked to the BE
database to compact
2. Deletes the links to these tables
3. Compacts and copies the BE database to a new name in the same
folder as the BE database
4. Relinks the tables to the FE database
Unfortunately this is the only way I seem to be able to do it. I would
appreciate any advice and or feedback on the function.
** START OF CODE **************************
'this constant replaces ".mdb" in strDatabaseName
Public Const DATABASE_COPY_NAME = "_TEST.mdb"
'strDatabaseName is the full path and name of the linked BE database
Function createDatabaseCopy(strDatabaseName As String) As Boolean
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strDBDestinationName As String
Dim colTbl As New Collection
Dim i As Integer
Set db = CurrentDb
'Add the names of the tables that are linked to the collection
For Each tdf In db.TableDefs
If tdf.Attributes = dbAttachedTable Then
If InStr(1, tdf.Connect, strDatabaseName, vbTextCompare) >
0 Then
colTbl.Add tdf.Name, tdf.Name
End If
End If
Next
'Delete the linked tables using the collection
For i = 1 To colTbl.Count
db.TableDefs.Delete colTbl(i)
Next
db.TableDefs.Refresh
Application.RefreshDatabaseWindow
strDBDestinationName = Replace(strDatabaseName, ".mdb",
DATABASE_COPY_NAME)
'Must remove the destination database if it exists prior to
compacting
If Not Dir(strDBDestinationName, vbNormal) = "" Then Kill
strDBDestinationName
DoEvents
DoEvents
'Now that links are removed, you can compact/copy the database
CompactDatabase strDatabaseName, strDBDestinationName
DoEvents
DoEvents
'relink the tables again using the collection
For i = 1 To colTbl.Count
DoCmd.TransferDatabase acLink, "Microsoft Access",
strDatabaseName, acTable, colTbl(i), colTbl(i)
Next
createDatabaseCopy = True
ExitPoint:
On Error Resume Next
db.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set tdf = Nothing
Set db = Nothing
Exit Function
ErrorHandler:
MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf &
"createDatabaseCopy" & vbCrLf & "modCopyDatabase"
Resume ExitPoint
End Function
** END OF CODE **************************
linked database to a new database. I need to copy the linked database
to a website so I need to be able to create a compacted copy of the
database. As it was linked, it wouldnt let me do it from the front
end. As a result, I have come up with the following routine that does
the following:
1. Gets a list of all the tables that are currently linked to the BE
database to compact
2. Deletes the links to these tables
3. Compacts and copies the BE database to a new name in the same
folder as the BE database
4. Relinks the tables to the FE database
Unfortunately this is the only way I seem to be able to do it. I would
appreciate any advice and or feedback on the function.
** START OF CODE **************************
'this constant replaces ".mdb" in strDatabaseName
Public Const DATABASE_COPY_NAME = "_TEST.mdb"
'strDatabaseName is the full path and name of the linked BE database
Function createDatabaseCopy(strDatabaseName As String) As Boolean
On Error GoTo ErrorHandler
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strDBDestinationName As String
Dim colTbl As New Collection
Dim i As Integer
Set db = CurrentDb
'Add the names of the tables that are linked to the collection
For Each tdf In db.TableDefs
If tdf.Attributes = dbAttachedTable Then
If InStr(1, tdf.Connect, strDatabaseName, vbTextCompare) >
0 Then
colTbl.Add tdf.Name, tdf.Name
End If
End If
Next
'Delete the linked tables using the collection
For i = 1 To colTbl.Count
db.TableDefs.Delete colTbl(i)
Next
db.TableDefs.Refresh
Application.RefreshDatabaseWindow
strDBDestinationName = Replace(strDatabaseName, ".mdb",
DATABASE_COPY_NAME)
'Must remove the destination database if it exists prior to
compacting
If Not Dir(strDBDestinationName, vbNormal) = "" Then Kill
strDBDestinationName
DoEvents
DoEvents
'Now that links are removed, you can compact/copy the database
CompactDatabase strDatabaseName, strDBDestinationName
DoEvents
DoEvents
'relink the tables again using the collection
For i = 1 To colTbl.Count
DoCmd.TransferDatabase acLink, "Microsoft Access",
strDatabaseName, acTable, colTbl(i), colTbl(i)
Next
createDatabaseCopy = True
ExitPoint:
On Error Resume Next
db.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set tdf = Nothing
Set db = Nothing
Exit Function
ErrorHandler:
MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf &
"createDatabaseCopy" & vbCrLf & "modCopyDatabase"
Resume ExitPoint
End Function
** END OF CODE **************************