Jumat, 29 April 2016

Creating a Replica & Partial Replica



Creating a Replica

The following listings demonstrate how to create a full, read/write replica of an existing replica using DAO and then using JRO.
DAO
Function DAOMakeAdditionalReplica(strReplicableDB As String, _
   strNewReplica As String) As Integer

   Dim dbsTemp As DAO.Database

   Set dbsTemp = DBEngine.OpenDatabase(strReplicableDB)

   dbsTemp.MakeReplica strNewReplica, "Replica of " & strReplicableDB

   dbsTemp.Close
End Function

JRO
Function JROMakeAdditionalReplica(strReplicableDB As String, _
   strNewReplica As String) As Integer

   Dim repMaster As New JRO.Replica

   repMaster.ActiveConnection = strReplicableDB

   repMaster.CreateReplica strNewReplica, "Replica of " & _
      strReplicableDB

   Set repMaster = Nothing
End Function

The code for creating a replica with JRO is similar to the DAO code. Both examples begin with opening or connecting to the design master. In DAO, the design master is opened with the DBEngine object's OpenDatabase method. In ADO, setting the Replica object's ActiveConnection property opens the design master. Once it is open, the new replica is creating by calling a method to create the replica. The JRO equivalent to the DAO MakeReplica method is CreateReplica.
The DAO MakeReplica method has an optional parameter named Options. This parameter allows you to indicate the type of replica to create: full or partial, read-only or read/write.
In JRO there are two optional parameters named Type and Updatability. The Type parameter allows the user to indicate whether the replica should be full or partial. The Updatability parameter allows the user to indicate whether the replica is read-only or fully updatable.
The following table shows how the optional parameters and constants for the DAO MakeReplica method map to those for the JRO CreateReplica method.
DAO Parameter
DAO Constant
JRO Parameter
JRO Constant
Options
dbRepMakePartial
Type
jrRepTypePartial
Options
dbRepMakeReadOnly
Updatability
jrRepUpdReadOnly

The JRO CreateReplica method has two additional, optional parameters named Visibility and Priority. These parameters are omitted in the JRO code example above indicating that the default value should be used. Visibility and Priority are new in JRO and provide additional control over how synchronizations with the replica will be performed. The default value for each of these parameters maps to the DAO behavior. See the section, "New Features in JRO" for more information about replica visibility and priority.

Creating a Partial Replica

Sometimes, it is necessary to create replicas that contain a subset of the data contained in another replica. For example, a business might store its entire sales database at the headquarters office but replicate only regional data to its regional offices across the country. You can create a separate replica for each regional office that contains only the data relating to that region. The database at the headquarters office would be a full replica, with which each partial replica would be synchronized.
There are two ways to filter the data in a partial replica. The first method is by an expression, similar to an SQL WHERE clause (without the word WHERE). With an expression-based filter, the records in the table are limited to those that satisfy the expression. The second method to filter data is with a relationship filter. Relationship filters allow you to enforce the relationship when replicating data. It is generally used in conjunction with an expression-based filter.
The following listings demonstrate how to create a new partial replica and then populate the data in the partial replica limited by both an expression based filter and a relationship based filter.
DAO
Sub DAOCreatePartial()
   Dim dbsFull As DAO.Database
   Dim dbsPartial As DAO.Database
   Dim tdfCustomers As DAO.TableDef
   Dim relCustOrders As DAO.Relation

   ' Create partial replica.
   Set dbsFull = DBEngine.OpenDatabase(".\NorthWind.mdb")
   dbsFull.MakeReplica ".\FY96.mdb", "Partial Sales Replica", _
      dbRepMakePartial
   dbsFull.Close

   ' Create an expression based filter in the partial replica.
   Set dbsPartial = DBEngine.OpenDatabase(".\FY96.mdb", True)
   Set tdfCustomers = dbsPartial.TableDefs("Customers")
   tdfCustomers.ReplicaFilter = "Region = 'CA'"

   ' Create a filter based on a relationship in the partial replica.
   Set relCustOrders = dbsPartial.Relations("CustomersOrders")
   relCustOrders.PartialReplica = True

   ' Repopulate the partial replica based on the filters.
   dbsPartial.PopulatePartial ".\NorthWind.mdb"

   dbsPartial.Close
End Sub

JRO
Sub JROCreatePartial()
   Dim repFull As New JRO.Replica
   Dim repPartial As New JRO.Replica

   ' Create partial replica.
   repFull.ActiveConnection = ".\NorthWind.mdb"
   repFull.CreateReplica ".\FY96.mdb", "Partial Sales Replica", _
      jrRepTypePartial
   Set repFull = Nothing

   ' Create an expression based filter in the partial replica.
   ' The PopulatePartial method requires an exclusive connection
   repPartial.ActiveConnection = _
      "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=.\FY96.mdb;Mode=Share Exclusive"
   repPartial.Filters.Append "Customers", jrFilterTypeTable, _
      "Region = 'CA'"

   ' Create a filter based on a relationship in the partial replica.
   repPartial.Filters.Append "Orders", jrFilterTypeRelationship, _
      "CustomersOrders"

   ' Repopulate the partial replica based on the filters.
   repPartial.PopulatePartial ".\NorthWind.mdb"

   Set repPartial = Nothing
End Sub

The process for creating a partial replica is the same in JRO as it is in DAO. With both models the process is as follows: create the partial replica, create the filter(s), populate the partial replica using the filters. The primary difference between the two models is in creating the filters. DAO exposes properties of the Table and Relation objects for creating filters. JRO has a Filters collection. Use the Filters collection Append method to create new filters.

Listing Filters

The following listings demonstrate how to list all of the filters for a partial replica.
DAO
Sub DAOListFilters()
   Dim dbPartial As DAO.Database
   Dim tbl As DAO.TableDef
   Dim rel As DAO.Relation

   Set dbPartial = DBEngine.OpenDatabase(".\FY96.mdb")

   For Each tbl In dbPartial.TableDefs
      If tbl.ReplicaFilter <> "" Then
         Debug.Print tbl.Name & " : Table Filter : " & _
            tbl.ReplicaFilter
      End If
   Next

   For Each rel In dbPartial.Relations
      Debug.Print rel.Name & " : Relationship Filter";
      If rel.PartialReplica Then
         Debug.Print " : Partial";
      End If
      Debug.Print
   Next

   dbPartial.Close
End Sub

JRO
Sub JROListFilters()
   Dim repPartial As New JRO.Replica
   Dim flt As JRO.Filter
   Dim strFilterType As String

   repPartial.ActiveConnection = _
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\FY96.mdb"

   For Each flt In repPartial.Filters
      If flt.FilterType = jrFilterTypeTable Then
         strFilterType = "Table Filter"
      Else
         strFilterType = "Relationship Filter"
      End If
      Debug.Print flt.TableName & " : " & strFilterType & " : " & _
         flt.FilterCriteria
   Next

   Set repPartial = Nothing
End Sub


EmoticonEmoticon