Synchronize the ACCESS database

Source: Internet
Author: User
Tags mdb database
Synchronization is an important concept involved in Database Application in the network environment. The basic process is roughly divided into the following steps: first, set a database as a copy Copy attribute to make it the original design (the original design in VB and the master design in access ); then, according to the implementation of the application, multiple copies are copied from the original design (known as copies in VB ), these replicas form a replica set (the original design is also considered as the first initial copy ); when the data or structure of any replica in the replica set is changed, the synchronization mechanism is enabled to send the change and apply it to other Members in the replica set, so that the Members in the replica set maintain consistency in the data or structure. Synchronization is called synchronization. To achieve synchronization, VB6.0 provides multiple attributes and methods in the database object to implement this process. The following describes the main attributes and methods, corresponding to the synchronization steps:

1. replicable attributes:

The replicable attribute is used to make a database object, table object, query object, and other objects in the Database become replicasable copies, that is, the original design. However, the database object does not provide the replicable attribute. Therefore, you must first create it using the createpropety method, add it to the attribute set of the object, and assign a value to it to make the database the original design. For database objects, setting the replicable attribute to "T" will make the database objects reproducible. The following code makes the nwind. mdb database attached to the VB6.0 installation directory an original design (to ensure security, we recommend that you back up this library file before the operation ):

Private sub commandementclick ()
Dim dbnwind as database
'If Dao is referenced at the end, it must be referenced first.
Dim prpnew as property
Set dbnwind = opendatabase ("nwind. mdb", true)
With dbnwind
'Create the replicable property. If this property already exists, the program skipped this step.
On Error resume next
Set prpnew =. createproperty ("replicable", dbtext, "T ")
. Properties. append prpnew
'Set the database's replicable attribute to true
. Properties ("replicable") = "T"
. Close
End
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 the path name of a new copy; description is a description string for the new copy being created; options is an optional, it can be a dbrepmakepartial constant (creating a partial copy) or a dbrepmakereadonly constant (preventing users from modifying the replicable object in a new copy). If you want to create a read-only partial copy, add the parameter constant dbrepmakereadonly + dbrepmakepartial.

In the first example, add the code before shutting down the database :. makereplica "nwreplica", "replica of nwind. mdb ", then from nwind. the original design of MDB copies a file named nwreplica. copy of MDB, which is located in nwind. MDB in the same directory. The following is a function that can be flexibly called in actual applications by passing parameters. Each time a function is called, a new copy can be created:

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 a parameter is provided at the end of intoptions when this function is called, this parameter is ignored,
'By default, a complete, read/write copy is created. Otherwise, a copy is created using the provided parameters as required.
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 copies (including the original design. Its syntax is: database. Synchronize pathname, exchange. Here, pathname is the path name string of the target copy to be synchronized (in the string. MDB extension can be omitted); Exchange is used to identify the synchronization direction between two databases (such as table 1). This is an option. By default, it is the third option in the table, that is, bidirectional exchange. Using the fourth dbrepsyncinternet constant option in the table, you can also synchronize databases connected through the Internet,
In this case, the pathname representing the local network path option should be replaced by the URL address.

Table 1. Synchronization direction Constants

Constant Synchronization direction
Dbrepexportchanges Path name from database to copy
Dbrepimportchanges From copy path name to database
Dbrepimpexpchanges Bidirectional switching (default)
Dbrepsyncinternet Transfer changes between databases connected through the Internet path

Before the synchronization operation, make sure that you have used the replicable attribute to make the initial design of a database and copied more than one copy using the makereplica method.

The following statement is added after the copy statement added in the first example :. synchronize "nwreplica. mdb ", dbrepexportchanges, to transfer any changes to the original design of the database nwind to the copy nwreplica. We can go to nwind. when the MDB library changes some data content and then runs this example, we will find that nwind. changes to the MDB Library have been reflected in nwreplica. the MDB copy is in progress.

The preceding statement synchronizes the path name from the database to the copy (the original data or structure change is passed to the copy ), change the constant dbrepexportchanges to dbrepimportchanges and dbrepimpexpchanges to synchronize the path name from the copy to the database (the database receives the changes on the copy) and bidirectional exchange (two-way data transfer between the two.

The synchronize method can also synchronize databases connected through the Internet. The following statement synchronizes the original local database with a copy on the internet server: dbnwind. synchronize "www.mycompany.myserver.com" & "/files/nwreplica. mdb ", dbrepimpexpchanges + dbrepsyncinternet

4. populatepartial method:

The preceding section uses the synchronize method to synchronize two full replicas without any issues. However, if you use a full copy to synchronize one part of the copy in the same step, because some replicas are filtered and regenerated from full replicas by replica filters, so-called "isolated" records may be generated in some replicas, that is, these records cannot be synchronized with other copies. To solve this problem, another method called populatepartial is introduced. This method is similar to the synchronize method, except that it synchronizes partial copies and full copies. In the same step, first, all records in some replicas are cleared, and then some replicas are regenerated Based on the filters of the current replicas. This solves the "isolated" record problem. The syntax is database. populatepartial dbname. Dbname is the path name of the complete copy. Due to space limitations and their similarity with the synchronize method, I will not describe it here. For more details, see related online help.

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.