> You probably mean DSN ... No, it's the same Driver
Yes, I meant DSN (could'nt recall the name at the moment of posting).
While we are at it (eh eh): I also have an SdfDataSet, from where I should extract certain records and, if they don't exist in the MDB table, add them there. So, look in the SDF for records having some criteria, determine if they should be appended or not in MDB (are they missing in MDB?): if yes, append.
The last part (appending record) is the easy one, I think I have understood.
Browsing the SDF should be easy with TDataSet methods (First, Next, FielddByName etc).
Checking whether a record (with a unique ID) exists in the MDB raises some doubt. Should I use an SQL query and count the number of records returned (just 1 or 0)? Or is there another method?
Again, thanks to all.
Since MS Access doesn't support INSERT IGNORE (like MySQL) (Have a guess what INSERT IGNORE does....) your only way is to check beforehand
As for checking if a Record exists in the DB: I'd turn it around: Check which one DOESN'T exist, and INSERT directly.
Should work with a LEFT JOIN with a IS NULL in the WHERE-CLAUSE
No idea, if MS Access supports JOINs in INSERTs, either...
OTOH, you could switch off Error-Handling (try...except/finally), and just fire off the INSERT, irrespective if it exists or not.
If it exists, you'll get an Exception you can ignore, and we already had that discussion in this Forum
EDIT: Just looked it up: MS Access supports JOIN's in INSERTS, but it's a bit convoluted.
Anyway, it would do away with the need to lookup beforehand if an ID exists
EDIT2: As a "concept" how i would approach this:
1) Create a String-Array. RowCount is the Same as your SDF-Dataset, Columns as many as you want to insert into the Access-DB (Optional!!!)
2) Run through your SDF "collecting" the Values into that StringArray, in the Format "SELECT ValueOfID As AliasOfID, ValueOfCol1 As AliasOfCol1, ValueOfCol2 As AliasOfCol2....". "SELECT" is hardcoded! The Alias is only needed at the first Array-Member
Note: You have to "quotify" columns where neccessary
3) Join the String-Array with Delimiter " UNION ALL " (Note the Spaces before UNION and after ALL!!!)
This results in a String e.g.
SELECT 1 AS ID, 'John' AS FirstName, 'Smith' AS LastName UNION ALL SELECT 2, 'Joe', 'Miller' UNION ALL SELECT 3, 'Jane', 'Doe'
Then it would be e.g. (Aircode)
INSERT INTO MyAccessTable(ID, FirstName, LastName)
SELECT T.ID, T.FirstName, T.LastName FROM
(SELECT 1 AS ID, 'John' AS FirstName, 'Smith' AS LastName UNION ALL SELECT 2, 'Joe', 'Miller' UNION ALL SELECT 3, 'Jane', 'Doe') AS T
LEFT JOIN MyAccessTable AS M ON M.ID=T.ID
WHERE M.ID IS NULL
If record 2 (Joe Miller) already exists in your MDB, it gets filtered out
Upside with this Approach: You can test everything after the INSERT, if it returns the correct Records before you go live in production with this.