?
Solved

in MS Access 2010 prevent Append query from creating duplicate entries

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

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 48

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

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 38

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

764 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