Solved

in MS Access 2010 prevent Append query from creating duplicate entries

Posted on 2016-08-17
6
129 Views
Last Modified: 2016-08-21
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!
0
Comment
Question by:shogun5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 36

Expert Comment

by:PatHartman
ID: 41759563
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.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 41759803
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.
0
 

Author Comment

by:shogun5
ID: 41760305
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 250 total points
ID: 41760745
The queries I provided are basically what you are looking for.  Instead of simply appending students to the list, you must only append those which are not already there, for today's date.  So, instead of using the AfterUpdate event of cboDate mentioned in my earlier post, try an append query that looks like:
INSERT INTO tblAttendance (StudentID, InClass, Att_Date)
SELECT S.StudentID, S.InClass, Att_Date
FROM (
SELECT [StudentID], 0 as InClass, Date() as Att_Date, Date() as Att_Date
FROM tblStudent
) 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


You could run this code when the form opens, or even when you open your application.  Then, assuming that the sample form is a continuous form, you won't need any code associated with the click event of the checkbox, as this field will be bound to the form.

Dale

BTW, "Date" is a reserved word, so I would not use it as a field name in tblAttendance.  Make it a meaningful name Att_Date is used above.
0
 
LVL 36

Accepted Solution

by:
PatHartman earned 250 total points
ID: 41761181
I've attached an old membership database that might give you some ideas.  I don't recommend the funky color scheme but the user wanted it that way.

It does some interesting things such as picking a random number for a drawing and graphing attendance.
CaML2K-SanatizedData.zip
0
 

Author Closing Comment

by:shogun5
ID: 41764771
Thanks guys! Both suggestions helped!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question