?
Solved

in MS Access 2010 prevent Append query from creating duplicate entries

Posted on 2016-08-17
6
Medium Priority
?
375 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 40

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 49

Expert Comment

by:Dale Fye
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 49

Assisted Solution

by:Dale Fye
Dale Fye earned 1000 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 40

Accepted Solution

by:
PatHartman earned 1000 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

Technology Partners: 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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 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