The simplest way to do a complete copy of the table including
indexes, etc. is to use the TransferDatabase method. You can
export the table to another database if you wish. You just need
to change some argument values.
That won't copy relationships, though.
A way to do that is to use the undocumented Application.SaveAsText 6
(thanks, Lyle!). Code to use that is found after my signature.
I have this in production use, but it's not used often (it's used to
make a backup before an elaborate import routine is run), and I
intend to alter it to create the backup in the local PC's %temp%
folder and then move it to the location of the original database.
The reason is that in production use, I found that the first time
you backup the database across the network it's very slow (since it
has to pull everything across to the local workstation and then
write it back). It seems safer and simpler to do the backup on the
local machine, instead of writing it across the wire.
But I haven't gotten back to this yet to make that adjustment.
--
David W. Fenton
http://www.dfenton.com/
contact via website only
http://www.dfenton.com/DFA/
Public Function CreateBackup(strMDBName As String, _
strBackupPath As String, _
Optional ysnCompact As Boolean = False) As Boolean
On Error GoTo errHandler
Dim objAccess As Object
Dim strBackupMDB As String
Dim strCompactMDB As String
If Len(Dir(strBackupPath & "\*.*")) = 0 Then
'If Not FSO.FolderExists(strBackupPath) Then
MkDir strBackupPath
End If
Set objAccess = New Access.Application
objAccess.Application.OpenCurrentDatabase strMDBName
strBackupMDB = "Backup" & Format(Now(), "YYYYMMDDhhnnss") & ".mdb"
Debug.Print strBackupPath & "\" & strBackupMDB
' does not work on anything other than Application.CurrentDB()
objAccess.Application.SaveAsText 6, vbNullString, strBackupPath _
& "\" & strBackupMDB
objAccess.Application.Quit
Set objAccess = Nothing
If ysnCompact Then
strCompactMDB = strBackupPath & "\" & "c_" & strBackupMDB
Name strBackupPath & "\" & strBackupMDB As strCompactMDB
DBEngine.CompactDatabase strCompactMDB, strBackupPath _
& "\" & strBackupMDB
Kill strCompactMDB
End If
CreateBackup = (Len(Dir(strBackupPath & "\" & strBackupMDB)) > 0)
exitRoutine:
If Not (objAccess Is Nothing) Then
On Error Resume Next
objAccess.Application.Quit
On Error GoTo 0
Set objAccess = Nothing
End If
Exit Function
errHandler:
Select Case Err.Number
' Path/File access error -- MkDir on a folder that already
exists Case 75
Resume Next
Case Else
MsgBox Err.Number & ": " & Err.Description, vbExclamation, _
"Error in CreateBackup()"
Resume exitRoutine
End Select
End Function