Link to home
Start Free TrialLog in
Avatar of kmcbrearty
kmcbreartyFlag for United States of America

asked on

Synchronize Replicated Access Database using VB.NET

Can anyone help me to take the code below and convert it to something that I can use in VB.NET.  I don't really care how it works at this point just that it does.

    Function Synchronize(ByVal DB1 As String, ByVal DB2 As String)

        Call TwoWayDirectSync(DB1, DB2)

    End Function

    Private Sub TwoWayDirectSync(ByVal strReplica1 As String, ByVal strReplica2 As String)
        Dim repReplica As New JRO.Replica

        repReplica.ActiveConnection = strReplica1

        ' Sends changes made in each replica to the other.
        repReplica.Synchronize(strReplica2, jrSyncTypeImpExp, jrSyncModeDirect)
        repReplica = Nothing

    End Sub

Thank you for your help,
Kevin McBrearty
ASKER CERTIFIED SOLUTION
Avatar of Corey Scheich
Corey Scheich
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kmcbrearty

ASKER

I already tried that and it didn't work.  I recieved an error when the program got to repreplica.ActiveConnection.  I am not at home so I can't tell you the exact error.  I could possibly post it later, but this code does not work as is.

Just to make sure I did this correctly, an order to include the object library I added a reference to the project.  I located the file located at C:\Program Files\Common Files\System\ADO\msjro.dll.  
The error would help.  That sounds OK as far as the reference.

Did you try retyping the line to see what the auto code finisher comes up with.

You may want to add an include line something like

Include JRO '(Code finisher should help you out with that too.)

Corey2
I can post the error when I get home from work some time around 5:30pm.  After I retype the the line autocode comes up with ActiveConnection() as object I believe.  I can double check that as well.

>>You may want to add an include line something like

Are you refering to an actual include or an imports?
Sorry not imports but Inherits.
Include JRO
Include System

Public Class YourClass

'the subs above

End CLass

Or whatever your situation is.
Have you already made your database replicable

repReplica.MakeReplicable strDBPath, False


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnacc2k/html/jrorep.asp
The database itself is replicable.  I have used this exact code to synchronize the database using VBA within access and it works without a problem.  Since I have already defined the database itself as replicable I do not believe that I need to do it in the code, expecially since this particular code already works.

I will try to use the Include when I get home and see if that corrects the issue.
An include doesn't work.  Visual Studio.NET is indicating a syntax error.   I added the following imports:

Imports JRO
Imports JRO.SyncTypeEnum
Imports JRO.SyncModeEnum

The actual error that I am getting is:

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
I got it working.  The correct code is posted below.  I had to create an ADODB Connection and pass that to the activeconnection property.

-----------------------------------------------------------------------------------------------------------------------------------------------
Imports System
Imports System.Windows.Forms
Imports JRO
Imports JRO.SyncTypeEnum
Imports JRO.SyncModeEnum


Module JROSync

    Function Synchronize(ByVal DB1 As String, ByVal DB2 As String)

        Call TwoWayDirectSync(DB1, DB2)

    End Function

    Sub TwoWayDirectSync(ByVal strReplica1 As String, _
    ByVal strReplica2 As String)
        Dim repReplica As New JRO.Replica
        Dim DBConnection As ADODB.Connection
        Dim strDBConnection As String

        Try
            ' Open connection
            strDBConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strReplica1 & ";"
            DBConnection = New ADODB.Connection
            DBConnection.Open(strDBConnection)

            repReplica.ActiveConnection = DBConnection

            ' Sends changes made in each replica to the other.
            repReplica.Synchronize(strReplica2, jrSyncTypeImpExp, jrSyncModeDirect)

            repReplica = Nothing
            MessageBox.Show("Synchronization Complete", "Synchronize Database", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "An Error has Occured", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            DBConnection.Close()
            DBConnection = Nothing
        End Try

    End Sub

End Module
-----------------------------------------------------------------------------------------------------------------------------------------------

Thank you for keeping me on the right track.
Kevin McBrearty
Glad to be able to point you in the right direction.  Sorry, I didn't get a chance to post back over the weekend.  So it just wanted a connection object instead of a string object...makes sense.

Corey2