|
-
Dec 4th, 2017, 12:54 AM
#1
Thread Starter
New Member
Prevent Saving Duplicate Data To Database
I am using Visual Basic 6 with MS Access 2007. Here is the scenario,
i have to copy data from one database to another database. Let rs1 be the first Recordset, from where all the fields to be copied to the second recordset let rs2. There is a field ("Date") on both recordset. Now the condition is that if there already exist the data of a particular date on rs2, then it should skip the operation and goto next row and so on.
Need help doing this. Thanks in advance
-
Dec 4th, 2017, 05:16 AM
#2
Re: Prevent Saving Duplicate Data To Database
i suppose you mean copy data from a table in database1
to a table in database2
but only if the value of a date in the table in database1
does not already exist in the table of database2
if so, you have to put a unique constraint on the date in the table of database 2
and then you can run a simple insert into in query
example (since i do not have vb6 here, example in access)
Code:
Sub Test()
Dim S As String
S = "insert into Table1 in 'E:\Biblio22.mdb' select * from Table1"
CurrentDb.Execute S
End Sub
do not put off till tomorrow what you can put off forever
-
Dec 4th, 2017, 09:10 AM
#3
Re: Prevent Saving Duplicate Data To Database
the only problem I see with that is what will happen when it encounters the first duplicate date? Will it just stop and throw a constraint error (my guess: yes) or will it keep going (my guess: no). What I would do is create a new table for staging, import the data into the staging table, then copy those records that have a data that is NOT in the current table, and then finally drop the staging table.
-tg
-
Dec 4th, 2017, 03:54 PM
#4
Re: Prevent Saving Duplicate Data To Database
it will always succeed,even if there is nothing to insert
the database.execute method fails silently, unless you tell it to report the error
if you want it to return an error if it fails, you have to call it like:
Code:
CurrentDb.Execute S,dbFailOnError
and trap the error
https://msdn.microsoft.com/en-us/lib...ffice.12).aspx
do not put off till tomorrow what you can put off forever
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|