Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using MS Access database. I want to update data value of another database base on their primary key. ControlNo(primary key of Database1) and Controlno(primary key of Database2). If the value of Database1 is being updated, so as well in Database2.

What I have tried:

This is my code:
Dim connstring As String
       connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database1.accdb;"
       Dim conn As OleDbConnection = New OleDbConnection(connstring)
       Dim updatescardempnum As String = "UPDATE Database1_Table [Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database2.accdb;].Database2_Table ON Database1_Table.fn = Database2_Table.FirstName SET Database1_Table.ControlNo = Database2_Table.Controlno"
       Dim cmdupdatescardempnum As New OleDbCommand(updatescardempnum, conn)
       conn.Open()
       cmdupdatescardempnum.ExecuteNonQuery()
       conn.Close()


The code above shows this error:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in UPDATE statement.
Posted
Updated 17-Nov-19 5:04am

You will need two separate connection strings and manage two connections.
Or you can try the method shown here How to Connect two or more Databases in Visual Basic? - Stack Overflow[^]
 
Share this answer
 
v2
Well...

If you are currently connected to the first database and you want to update the second database, you need to use IN clause[^]. Example usage of IN clause you'll find here: How to: Get Data from Multiple Workbooks using One OleDbConnection?[^]. This tip is for Excel, but you can use IN clause the same way in MS Access.

Note: i would strongly recommend to link a table in the second database[^], then you'll be able to update it as a local table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900