Keep the Access database synchronized

Source: Internet
Author: User
Tags filter constant mdb database access database

Synchronization (synchronization) is an important concept to be involved in the application of database in network environment. The basic process is roughly divided into the following steps: First, make a database to be a replica of the properties of the original design (called the original design in VB, Access is called the Design Master), and then according to the implementation of the application needs to copy from the original design of multiple copies (VB said copy), These replicas form a collection of replicas (the original design is also considered to be the first the initial copy), and then enable synchronization when the data or structure of any of the replicas in the set is changed to send the change and apply it to other members in this replica set, making the members of the copy set consistent in data or structure. The process of implementing synchronization is called Synchronization. VB6.0 for the realization of synchronization, in the database object to provide a number of properties and methods to achieve this process, the following introduction of several major attributes and methods, respectively, corresponding to the synchronization of several steps:

1. Replicable properties:

The Replicable property is used to make a replica of a database object or a Table object in a database, a query object, and so on, and becomes the original design. However, the database object does not provide the Replicable property, so you first create it with the Createpropety method, add it to the object's attribute set, and then assign it to the original design. For database objects, setting the Replicable property to "T" will make the database object replicable. The following code will make the Nwind.mdb database that comes with the VB6.0 installation directory a design original (to back up this library file before the operation to ensure security recommendations):


Private Sub Command1_Click ()
Dim Dbnwind as Database
' If you refer to DAO at the end, you must first refer to
Dim Prpnew as Property
Set Dbnwind = OpenDatabase ("Nwind.mdb", True)
With Dbnwind
' Establishes the Replicable property, and if the property already exists, the program skips this step
On Error Resume Next
Set prpnew =. CreateProperty ("Replicable", Dbtext, "T")
. Properties.append prpnew
' Set the Replicable property of the database to True
. Properties ("replicable") = "T"
. Close
End With
End Sub

  2. MakeReplica Method:

The MakeReplica method copies a new full copy from the original design. Its syntax is: database. MakeReplica replica, description, options, where replica is a string representing a new replica path name; Description is a descriptive string of the new replica being created; options are an option, Can be a dbRepMakePartial constant (creating a partial replica) or a Dbrepmakereadonly constant (prevents the user from modifying the Replicable object in the new replica), and if you want to create a partial copy that is read-only, add the parameter constant Dbrepmakereadonly + dbrepmakepartial.

In the first example, add code before closing the database:. MakeReplica "Nwreplica", "Replica of Nwind.mdb", copies a copy named Nwind.mdb from the original Nwreplica.mdb design, located in the same directory as Nwind.mdb. The following is a function that can be called flexibly in practical applications by passing parameters, each time a function is invoked to create a new copy:

Function Makeadditionalreplica (strReplicableDB As String, Strnewreplica as String, intoptions as Integer) As Integer
Dim Dbstemp as Database
On Error GoTo ErrorHandler
Set dbstemp = OpenDatabase (strreplicabledb)
' If the argument is intoptions at the end of the call, the argument item is ignored.
' Default to create a full, readable/writable copy, or create a copy on demand using the supplied parameters
If intoptions = 0 Then
Dbstemp.makereplica Strnewreplica, "Replica of" & strReplicableDB
Else
Dbstemp.makereplica Strnewreplica, "Replica of" & strReplicableDB, Intoptions
End If
Dbstemp.close
ErrorHandler:
Select Case ERR
Case 0:
Makeadditionalreplica = 0
Exit Function
Case Else:
MsgBox "Error" & ERR & ":" & Error
Makeadditionalreplica = ERR
Exit Function
End Select
End Function

 3. Synchronize method:

The Synchronize method synchronizes two full replicas, including the original design. Its syntax is: database. Synchronize pathname, Exchange. Where pathname is the path name string for the destination replica to synchronize (the. mdb extension in the string is omitted); Exchange identifies the direction of synchronization between two databases (for example, table I), which is an option that defaults to the third option in the table, that is, two-way swapping. Using the fourth Dbrepsyncinternet constant option in the table, you can also synchronize databases that are connected over the Internet.
Instead, use the URL address to represent the local network path option pathname.

  Table I, Synchronization direction constants

Constant Synchronization direction
dbRepExportChanges From database to replica path name
Dbrepimportchanges From replica path name to database
Dbrepimpexpchanges Bidirectional exchange Change (default)
Dbrepsyncinternet Passing changes between databases that are connected over an Internet path

Before synchronizing, make sure that you have used the Replicable property to initialize a database to the original design and that you have copied more than one copy using the MakeReplica method.

Following the copy of the statement by the replica added in the first example, add the following statement:. Synchronize "Nwreplica.mdb", dbRepExportChanges, implements any changes to the original design of the database nwind to the copy Nwreplica. We can change some of the data content in the Nwind.mdb library, and then run this example, we will find that the Nwind.mdb library changes have been reflected in the Nwreplica.mdb copy.

The above statement implements the synchronization from the database to the replica path name (the data or structural changes to the original design are passed to the copy). Changing the dbRepExportChanges constant to Dbrepimportchanges and dbrepimpexpchanges can be implemented separately from the replica path name to the database (changes on the database receive copy) and the two-way exchange (two-way data transfer between the two).

The Synchronize method can also synchronize databases that are interconnected via the Internet, and the following statement synchronizes the original local database with a replica located on an Internet server: Dbnwind.synchronize " Www.mycompany.myserver.com "&"/files/nwreplica.mdb ", Dbrepimpexpchanges + dbrepsyncinternet

  4. PopulatePartial Method:

The above describes the use of the Synchronize method to synchronize two full replicas. The problem does not occur, but if a partial replica is synchronized with a full copy, because the partial replica is filtered from the copy filter to regenerate, it may produce a so-called "orphaned" record in a partial replica. That is, these records can no longer be synchronized with other replicas. To solve this problem, we introduce another method called PopulatePartial, which is similar to the Synchronize method, except that it is to synchronize a partial replica with a full replica, and, when synchronizing, first clear all the records in the partial replica. The partial replica is then regenerated based on the current copy's filter, which resolves the problem of "orphaned" records. Its syntax is: database. PopulatePartial dbname. DBName is the path name of the full replica. Due to space limitations and its similarity to the Synchronize method, this is no longer described here, please refer to the related online help for a more detailed description.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.