Solved

in MS Access 2010 prevent Append query from creating duplicate entries

Posted on 2016-08-17
6
53 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 34

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 34

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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now