KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to prevent having a duplicate record when importing a file?
PRODUCT: 4D | VERSION: 6.5 | PLATFORM: Mac & Win
Published On: October 11, 2001
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

The following is a simple way to prevent from having a duplicate record in your database when importing data from a file. In some cases, the import file contains the same exact records that you have in your data file. The only difference could be that it has the most recent updated information. When you import a file into your database, 4D will not overwrite an existing record with the new data. The data from the file will be created as a new record. Therefore, you need to find a way to get rid of the old record so that all records are unique and containing the most recent information.

Here is an example code that will import a text file into the database and perform an update to existing records.

C_LONGINT($maxSet;$TempAge;$newRecNum;$oldRecNum)
C_TEXT($TempName;$TempSex;$key)
C_BOOLEAN($Done)

$Done:=False

IMPORT TEXT([Table 1];"")
CREATE SET([Table 1];"tempSet")
$maxSet:=Records in selection([Table 1])

For ($i;1;$maxSet)
USE SET("tempSet")
GOTO SELECTED RECORD([Table 1];$i)
$newRecNum:=Record number([Table 1])

` The most important part of the procedure.
` The designer must design exactly criteria that will
` determine the record to be duplicated
$key:=[Table 1]Name
QUERY([Table 1];[Table 1]Name=$key)

If (Records in selection([Table 1])>1)
While ((Not($Done)) & (Not(End selection([Table 1]))))
If (Not(Is in set("tempSet")))
$oldRecNum:=Record number([Table 1])
GOTO RECORD([Table 1];$newRecNum)
$TempName:=[Table 1]Name
$TempSex:=[Table 1]Sex
$TempAge:=[Table 1]Age
GOTO RECORD([Table 1];$oldRecNum)
[Table 1]Name:=$TempName
[Table 1]Sex:=$TempSex
[Table 1]Age:=$TempAge
SAVE RECORD([Table 1])
$Done:=True
End if
NEXT RECORD([Table 1])
End while
$Done:=False
End if
End for

USE SET("tempSet")
DELETE SELECTION([Table 1])
CLEAR SET("tempSet")

Note: This code is given as an example; it is not generic to all projects. To use this code in your own project, make sure that the search criteria is modified to match your requirements.