Link to home
Start Free TrialLog in
Avatar of Mike Rudolph
Mike RudolphFlag for United States of America

asked on

in MS Access 2010 prevent Append query from creating duplicate entries

Dear Experts

I am trying to create  an attendance form for my student database. I found a tutorial here:
https://www.youtube.com/watch?v=DbBky5mNGE4

...and while it works great the first time I run the append query. If a student arrives late and I click on the students [present] check box on the tabular form and run the append query again I get the studenti name, current date, and present (yes/no field) posted again in the table. If I run the query again then each record is in the table three times, etc.

I do I only update the "present" check field on the student who previously did not have a check and leave other students alone.

I tried creating an index composite as described below:

http://en.allexperts.com/q/Using-MS-Access-1440/Creating-Unique-Value-check.htm

but that just prevent any updates at all.

Any suggestions?

Thanks!
Avatar of PatHartman
PatHartman
Flag of United States of America image

Append and update are two different operations.  Append adds rows to a table.  Update modifies existing rows.  To prevent "duplicates" from being added to a table, create a unique index on the combination of columns that defines uniqueness so following the directions to create the composite index was correct.  Please post a picture of the Index dialog so we can see what you chose.  Make sure the first line is highlighted so we can see the properties and make sure we can also see the table.

To handle the late comers, create a form that selects the attendance records for today and then scroll to the record you want to update and check the attended box.
I agree with Pat that you should have a unique index on the attendance data and the studentID.

But to go a step further, and only attempt to append records which do not already exist, thereby preventing an error from occuring.  I generally do this with a combo box (or possibly a textbox) to select the date.  Then, in the AfterUpdate event of that control, I insert all of the students into the Attendance table (assumes field ID, Att_Date, StudentID, IsPresent) based on a query which might look like the following:
INSERT INTO tblAttendance (Att_Date, StudentID, IsPresent)
SELECT S.Att_Date, S.StudentID, S.IsPresent
FROM (
SELECT Eval([Forms]![YourFormName]![cboDate]) as Att_Date, [StudentID], 0 as IsPresent
FROM tblStudents WHERE IsActive = -1
) as S
LEFT JOIN tblAttendance as A
ON S.Att_Date = A.Att_Date AND S.StudentID = A.StudentID
WHERE A.ID IS NULL

Open in new window


This query starts out with a subquery that joins the date selected on your form to the list of active students, it then uses the LEFT JOIN to link to the current Attendance table and filter out those records which already exist, then it inserts the remaining records into tblAttendance.

if you set the default value of the IsPresent field in your Attendance table to False (0) then you can remove the references to the IsPresent field like:
INSERT INTO tblAttendance (Att_Date, StudentID)
SELECT S.Att_Date, S.StudentID
FROM (
SELECT Eval([Forms]![YourFormName]![cboDate]) as Att_Date, [StudentID]
FROM tblStudents WHERE IsActive = -1
) as S
LEFT JOIN tblAttendance as A
ON S.Att_Date = A.Att_Date AND S.StudentID = A.StudentID
WHERE A.ID IS NULL

Open in new window


Then I would have a subform which displays all of the students with the IsPresent field as a checkbox based on a RecordSource of:
SELECT A.IsPresent, S.Student_LastName & ", " & S.Student_FirstName
FROM tblAttendance as A
INNER JOIN tblStudents as S ON A.StudentID = S.StudentID
WHERE A.Att_Date = [Forms]![YourFormName]![cboDate]
ORDER BY S.Student_LastName & ", " & S.Student_FirstName

Open in new window


Finally, after inserting the date/studentID combinations into tbl_Attendance, I would execute a requery on the subform.
Avatar of Mike Rudolph

ASKER

Pat and Dale,

Okay...attached are screenshots of how I am attempting to update the attendance. Perhpas there is a better way as I am learning to design this database. Pat has already provided some helfpul pointers on structure. And Dale thanks for the code above. Can I make this work with the current setup. See attachments or should I set up the attendance form differently? Do you have or can link me to a sample of how someone has created a nice attendance form?

Thanks!
screenshots.pdf
SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks guys! Both suggestions helped!