views:

390

answers:

2

I'm working on an MS Access Database with tons of duplicate entries. The problem is that there is a table of students, and sometimes instead of just updating a certain student's information, someone would just add the student in again with a different ID. I want to get rid of all the duplicates (which is a pain since there's barely any way to differentiate them), which would be fine by just deleting the duplicates, except that other tables may rely on the duplicate. How can I change all the tables that rely on a certain ID to rely on the ID that I choose to keep?

Here's what it looks like:

 Student ID | L. Name |F. Name

 ANDY-01    | Andy    |  Andy

 ANDY-02    | Andy    |  Andy

Then in the courses table I'd have courses that ANDY-01 would have taken, and courses ANDY-02 would have taken. I want to merge all entries in all the tables that would have ANDY-01 and ANDY-02 as ANDY-01. How would I go about this?

(Don't worry about how I'm differentiating between ANDY-01 and ANDY-02)

+2  A: 

You just have to make some update SQL:

update another_table set student_id=:ID2 where student_id=:ID1

Riho
Anyway I could make it update more than one table in a single query, or perhaps a way to do a bunch of queries at once (maybe using a different language (MSVB?))?
Jeff
@Riho: what's iwth the "=:"? That's not going to be properly executed by Jet/ACE. You *did* pay attention to what database was involved, right?
David-W-Fenton
:ID1 - parameter to be replaced with real value
Riho
And? That works exactly how with a Jet/ACE database?
David-W-Fenton
That is left as an exercise for the user. Answer is given by the Ben V.
Riho
+2  A: 

+1 for Riho's answer. To update multiple tables you could create a procedure like the one below, and manually update the ID values and execute the procedure for each student.
If you have a table or query that maps the old and new IDs you could write another procedure to read the table and call this procedure for each student.

Public Sub UpdateStudent()
    Dim oldID As String
    Dim newID As String

    oldID = "ID1"
    newID = "ID2"

    DoCmd.Execute "update another_table set student_id='" & newID & "' where student_id=" & oldID
    DoCmd.Execute "update yet_another_table set student_id='" & newID & "' where student_id=" & oldID
End Sub
BenV
I'm having a bit of trouble with my VB code. This is what i have: . Option Compare DatabasePublic Sub RemoveDupelicateDepartments()Dim oldID As StringDim newID As StringoldID = "DND-01"newID = "DEPA-04"DoCmd.RunSQL "UPDATE [Clean student table] SET [HomeDepartment]=" "End Sub . Whenever I run it, it asks me for the parameters "DEPA" and "DND". It works fine as an SQL query. Maybe I am declaring strings wrong in VB?
Jeff
Oh god that formatted poorly
Jeff
If your IDs are strings instead of integers then they need to be wrapped in quotes. I've updated my example to use strings. Notice the single quotes in the SQL statements.
BenV