Append query problems (1 Viewer)

Lol999

Registered User.
Local time
Yesterday, 18:04
Joined
May 28, 2017
Messages
184
Hi, having got my sql in some sort of order in a previous post I am trying to insert the sorted fields into another table.
Testing the code without the "Insert" clause it works fine, but when I come to try and insert the data into a table I get a warning from Access that it can't append all the files (see attached picture).

I have attached picture of the data as is appears in it's initial form imported from an excel s/s, and a picture of the data sorted as desired in the table but I need to get passed this hang up.

The fields Job Code, EmpName and WeekEnding are all set as a combined foreign key and are Indexed.

I have tried varying the Index properties for the fields in the table all to no avail.
Has anyone a direction they can point me in please?
Code as below:
Code:
INSERT INTO Tbl_Costing ( [Job Code], EmpName, HoursWorked, WeekEnding )
SELECT Tbl_Trial.[Job Code], Tbl_Trial.EmpName, Tbl_Trial.HoursWorked, Tbl_Trial.WeekEnding
FROM Tbl_Trial
GROUP BY Tbl_Trial.[Job Code], Tbl_Trial.EmpName, Tbl_Trial.HoursWorked, Tbl_Trial.WeekEnding
HAVING (((Tbl_Trial.[Job Code])>"") AND (NZ(Tbl_Trial.EmpName,'')<>''));
 

Attachments

  • Database Table Unsorted.jpg
    Database Table Unsorted.jpg
    66.7 KB · Views: 100
  • Database Append Warning.jpg
    Database Append Warning.jpg
    76.9 KB · Views: 87
  • Database Table Sorted.jpg
    Database Table Sorted.jpg
    49.8 KB · Views: 56

CJ_London

Super Moderator
Staff member
Local time
Today, 02:04
Joined
Feb 19, 2013
Messages
16,607
the clue is in the error message - 'didn't add 4 records due to key violations'

Keys are indexes - so the implication is the data you are trying to insert would break the indexing rules. These rules are based on your index and/or table design. Typically it will be because you are trying to insert duplicates when duplicates are not allowed or insert null values where nulls are not allowed - and this might also apply to fields in the destination table you are not appending a value to.

Note that (although your query isn't sorting, but you mention it in your post) sorting records prior to insert has no benefit - records are stored randomly within a table regardless of order of insert. If they do actually appear in the same order, that is a coincidence rather than by design.

Also you query should not be a group by query because you are not summing/counting etc data. And if you are using it as a way of removing duplicates then your having criteria should be a where criteria - or use select distinct instead
 

Lol999

Registered User.
Local time
Yesterday, 18:04
Joined
May 28, 2017
Messages
184
Okay, so I've pruned the sql somewhat to this, the idea being to remove all the blank cells before inserting them into the main table, Tbl_Costing:
Code:
INSERT INTO Tbl_Costing ( [Job Code], EmpName, HoursWorked, WeekEnding )
SELECT Tbl_Trial.[Job Code], Tbl_Trial.EmpName, Tbl_Trial.HoursWorked, Tbl_Trial.WeekEnding
FROM Tbl_Trial
WHERE ((NZ(Tbl_Trial.[Job Code],''))<>'') AND ((Nz([Tbl_Trial].[EmpName],''))<>'');
I understand the key violations warning but couldn't understand why I was getting it. in the table Tbl_Costing I have a 3 part foreign key comprising the fields Job Code, EmpName and WeekEnding which primarily is to stop users of the excel sheet sending the data multiple times.
The key doesn't seem to be working though as I can count 4 instances, same number in the key violation warning, where a EmpName occurs more than once, yet each time it has a different Job Code so in theory should be exempt from the key violations.
Bit puzzling this one.

SOLVED FOR NOW- WILL POST LATER IF ONLY A TEMPORARY FIX :)
 
Last edited:

Lol999

Registered User.
Local time
Yesterday, 18:04
Joined
May 28, 2017
Messages
184
Okay, here we go!
I pruned my sql code to this:
Code:
INSERT INTO Tbl_Costing ( [Job Code], EmpName, HoursWorked, WeekEnding )  SELECT Tbl_Trial.[Job Code], Tbl_Trial.EmpName, Tbl_Trial.HoursWorked, Tbl_Trial.WeekEnding  FROM Tbl_Trial WHERE Tbl_Trial.[Job Code] AND Tbl_Trial.EmpName IS NOT NULL;

I found out the frustrating way that Excel, from where I am sending data to a db, DOES NOT LIKE the Nz function, hence the IS NOT NULL.

I created a 3 way unique index of Job Code, EmpName and WeekEnding to prevent data being re-sent and it works, hallelujah!

So there we are, after all the blood and frustration it works.
Thanks to CJ for their help on this one.
 

Users who are viewing this thread

Top Bottom