Documente Academic
Documente Profesional
Documente Cultură
Option Explicit
Public Function UnReplicate() As Boolean
' This function copies or imports all the objects and database startup
' properties from a replicated database into an un-replicated database.
' It removes tablename_Conflict tables and removes replication-related
' fields like s_GUID, etc.
'
'
'
'
' The core logic for the table and query copy was modified from:
' http://www.gab2001uk.com/visualbasic/daovsado/daocopy.htm
On Error GoTo Err_UnReplicate
' FileDialog requires a reference to a Microsoft Office Object Library
' (10 [Access 2002] or later).
Dim fdlPick As Office.FileDialog
Dim varFileRep
Dim varFileNew
' DOA requires a reference to Microsoft DAO Object Library, or for
' Access 2007 and later, the Microsoft Office Access database engine
' Object Library.
' Database.
Dim dbRep As DAO.Database
Dim dbNew As DAO.Database
' For copying
Dim tblRep As
Dim tblNew As
Dim fldRep As
Dim fldNew As
Dim idxRep As
Dim idxNew As
End If
Next tblRep
'********************
Debug.Print "Copy Relationships"
'********************
' Loop through the collection of table relationships.
For Each relRep In dbRep.Relations
' Create a relation with the same name.
Set relNew = dbNew.CreateRelation(relRep.Name, relRep.Table, _
relRep.ForeignTable, relRep.Attributes)
' Loop through the collection of relation fields.
For Each fldRep In relRep.Fields
' Append a relation field with the same name.
relNew.Fields.Append relNew.CreateField(fldRep.Name)
' Give the relation field the same foreign name.
relNew.Fields(fldRep.Name).ForeignName = _
relRep.Fields(fldRep.Name).ForeignName
Next fldRep
' Append the the relation to the database.
dbNew.Relations.Append relNew
Next relRep
'********************
Debug.Print "Copy Queries"
'********************
' Loop through the collection of query definitions.
' We use this method rather than TransferDatabase action used below
' because both tables and queries are listed in the Tables container.
For Each qryRep In dbRep.QueryDefs
' Create a query definition with the same name and SQL.
Set qryNew = dbNew.CreateQueryDef(qryRep.Name, qryRep.SQL)
' Set properties.
qryNew.Connect = qryRep.Connect
qryNew.MaxRecords = qryRep.MaxRecords
qryNew.ReturnsRecords = qryRep.ReturnsRecords
' Append the query definition to the database (NOT NECESSARY).
' dbRep.QueryDefs.Append qryNew
Next qryRep
'********************
Debug.Print "Copy Startup Options"
'********************
' Create an array of startup options to examine.
avarSUOpt = Array( _
"AllowBreakIntoCode", _
"AllowBuiltInToolbars", _
"AllowFullMenus", _
"AllowShortcutMenus", _
"AllowSpecialKeys", _
"AllowToolbarChanges", _
"AppIcon", _
"AppTitle", _
"StartupForm", _
"StartupMenuBar", _
"StartupShortcutMenuBar", _
"StartupShowDBWindow", _
"StartupShowStatusBar")
' Handle errors in this section of code.
On Error Resume Next
' Loop through the array.
For intC = 0 To UBound(avarSUOpt)
' Get the name of the property from the array.
strSUOpt = avarSUOpt(intC)
' Clear and continue if there is an error.
Err.Clear
' Try to get the property in the replicated database.
Set prpRep = dbRep.Properties(strSUOpt)
If Err.Number = 0 Then
' The property exists in the replicated database.
' Get its Value and Type.
varValue = prpRep.Value
varType = prpRep.Type
' Try to get the property in the un-replicated database.
Set prpNew = dbNew.Properties(strSUOpt)
If Err.Number = 0 Then
' The property exists. Reset its value to the
' replicated database value.
prpNew.Value = varValue
Else
' The property does not exist yet. Create the property,
' using the replicated database type and value, and
' then append it to the database.
Set prpNew = dbNew.CreateProperty(strSUOpt, varType, varValue)
dbNew.Properties.Append prpNew
End If
Else
' The property does not exist in the replicated
' database, so ignore it.
End If
Next intC
' Turn overall error handling back on.
On Error GoTo Err_UnReplicate
' Close the un-replicated database so that it can be opened
' exclusively using GetObject.
dbNew.Close
' Get the Access application object for the un-replicated database.
Set appNew = GetObject(varFileNew)
appNew.Visible = False
'********************
idxRep
idxNew
fldRep
fldNew
=
=
=
=
Nothing
Nothing
Nothing
Nothing