Manipulating table records

  • Thread starter Thread starter Eric G
  • Start date Start date
E

Eric G

I have Table A with approx. 600 records.
Table B will have the exact same field setup as Table A, but have an
additional 70 some odd records.
Table B's additional 70+ records will not come at the end of the copy
of Table A, but rather be mixed in amongst the other records.

Would someone be able to tell me how I can sort Table B's records so
that the first 600 records are in the exact same order as in Table A
The order of the remaining 70+ records is not important.

The idea is to then take this newly sorted table of records and throw
them into Table C, a new table, taking advantage of C's fresh record
numbering.

Guidance would be greatly appreciated! Eric
 
Eric G said:
I have Table A with approx. 600 records.
Table B will have the exact same field setup as Table A, but have an
additional 70 some odd records.
Table B's additional 70+ records will not come at the end of the copy
of Table A, but rather be mixed in amongst the other records.

Would someone be able to tell me how I can sort Table B's records so
that the first 600 records are in the exact same order as in Table A
The order of the remaining 70+ records is not important.

The idea is to then take this newly sorted table of records and throw
them into Table C, a new table, taking advantage of C's fresh record
numbering.

Guidance would be greatly appreciated! Eric

Tables have no order. You present your data in a desired order by using a query
that sorts on specific fields. That being the case it matters not one but how
you get the records into the table.
 
Hi Rick,

Thanks for helping out!
Tables have no order. You present your data in a desired order by using a query
that sorts on specific fields. That being the case it matters not one but how
you get the records into the table.

Yes I understood this concept quite a while ago.

In my situation however, it is *quite* important what the order of the
records will be in the final table.

My exisiting Table A is related to other tables. For example StudentID
426 from Table A is locked into a record in another table that refers
to StudentID 426.

When the new data will be brought into Table B, StudentID 426 may be
'taken over' by another student.

So it is crucial that:
1. The records in Table B be first sorted to match the order of TableA
2. Have the remaining 70+ new records rearranged so that they
'physically' sit at the end of the table.

Thanks! Eric
 
1. The records in Table B be first sorted to match the order of TableA
2. Have the remaining 70+ new records rearranged so that they
'physically' sit at the end of the table.

That cannot be done... period.

The table has no order. IT REALLY HAS NO ORDER. Access will put new
records wherever there happens to be room on the disk; you have *no*
control over it. It's like putting 70 new marbles into a bag and
insisting that they be the last 70 marbles you take out.

You will need to update your student data in some other way.
 
That cannot be done... period.

The table has no order. IT REALLY HAS NO ORDER. Access will put new
records wherever there happens to be room on the disk; you have *no*
control over it. It's like putting 70 new marbles into a bag and
insisting that they be the last 70 marbles you take out.

You will need to update your student data in some other way.

John W. Vinson[MVP]

Hi John,

OK let me paint the picture another way.
Table A currently looks something like this:

ID LastN FirstN Rm1 Rm2 Rm3 Rm4
1 AAA Ron 10 12 14 15
2 ABC Tim 12 15 12 12
3 ACG Kim 8 15 6 14

ID's 1, 2, 3 etc. have already been linked to several other tables in
the db.

To the previous 600+ students, some 70+ have been added.
And the original 600+ students now have _different room numbers.
Some new students have a last name like ABB Pete.
I will be handed a .txt file with the new 670+ students in alpha
order.

Problem: How do I get all the names in a table that will replace
existing Table A, so that the *integrity of all the current ID links*
will be maintained?

Thanks! Eric
 
Hi John,

OK let me paint the picture another way.
Table A currently looks something like this:

ID LastN FirstN Rm1 Rm2 Rm3 Rm4
1 AAA Ron 10 12 14 15
2 ABC Tim 12 15 12 12
3 ACG Kim 8 15 6 14

ID's 1, 2, 3 etc. have already been linked to several other tables in
the db.

Ok... this table is INCORRECTLY DESIGNED already. You have a many
(student) to many (room) relationship embedded within each record. So
the new data problem is even WORSE than it looks! said:
To the previous 600+ students, some 70+ have been added.
And the original 600+ students now have _different room numbers.
Some new students have a last name like ABB Pete.
I will be handed a .txt file with the new 670+ students in alpha
order.

Can you count on the names to be unique? With that many students I
doubt it: if you have two records in the 670+ text file for ABB Pete,
or (more realistically) Jones Pete, and one Jones Pete in your current
table, WHICH ONE is the existing Jones Pete and which one is the new
Jones Pete? Alphabetizing won't help you here!
Problem: How do I get all the names in a table that will replace
existing Table A, so that the *integrity of all the current ID links*
will be maintained?

ASSUMING that the combination of LastN and FirstN uniquely identifies
a student (an assumption that makes me queasy at best), you can run
two queries:

- An Update query joining the (linked or imported) textfile to your
table by LastN and FirstN, updating all the Room fields
- An Append query appending records from an "Unmatched" query:

INSERT INTO yourtable
SELECT txtfile.LastN, txtfile.FirstN, txtfile.Rm1, txtfile.Rm2,
txtfile.rm3, txtfile.rm4
FROM yourtable LEFT JOIN textfile
ON yourtable.LastN = txtfile.LastN
AND yourtable.FirstN = txtfile.FirstN
WHERE txtfile.LastN IS NULL;

(assuming that the ID is an autonumber).
 
Hi John,
Ok... this table is INCORRECTLY DESIGNED already. You have a many
(student) to many (room) relationship embedded within each record. So
the new data problem is even WORSE than it looks! <wry grin>

You are correct about the poor design. It was great until sometime
last year when I quickly added Per1 Per2 Per3 and Per4 room numbers
for each student to help out a teacher tracking student location
during certain periods.
I guess I should have set up another table, perhaps 'Room' table and
linked the Students table to the Room table via StudentID ?
Is that a better way to go?
Can you count on the names to be unique? With that many students I
doubt it: if you have two records in the 670+ text file for ABB Pete,
or (more realistically) Jones Pete, and one Jones Pete in your current
table, WHICH ONE is the existing Jones Pete and which one is the new
Jones Pete? Alphabetizing won't help you here!

There may be a couple of dups. In the past, I went in at the start of
a semester and checked for dups, usually using a middle name to
differentiate.
ASSUMING that the combination of LastN and FirstN uniquely identifies
a student (an assumption that makes me queasy at best), you can run
two queries:

I will check for dups before importing and change first names
accordingly if I find any.
- An Update query joining the (linked or imported) textfile to your
table by LastN and FirstN, updating all the Room fields
- An Append query appending records from an "Unmatched" query:

INSERT INTO yourtable
SELECT txtfile.LastN, txtfile.FirstN, txtfile.Rm1, txtfile.Rm2,
txtfile.rm3, txtfile.rm4
FROM yourtable LEFT JOIN textfile
ON yourtable.LastN = txtfile.LastN
AND yourtable.FirstN = txtfile.FirstN
WHERE txtfile.LastN IS NULL;

(assuming that the ID is an autonumber).

Yes the ID field is autonumber.
OK, now let me see if I understand your code correctly.
The above code is for the Append query correct?
I have already imported the text file into Access and named it, say
'Table B', correct?
I'm trying to understand where in the query it finds 'unmatched'
records?
I don't quite understand WHERE txtfile.LastN IS NULL?

Thanks for your help! Eric
 
Hi John,


You are correct about the poor design. It was great until sometime
last year when I quickly added Per1 Per2 Per3 and Per4 room numbers
for each student to help out a teacher tracking student location
during certain periods.
I guess I should have set up another table, perhaps 'Room' table and
linked the Students table to the Room table via StudentID ?
Is that a better way to go?

TWO additional tables: a table of Rooms, and a table of
RoomAssignments. RoomAssignments should have (at least) two fields -
the student ID and the room number (or unique room ID).
Yes the ID field is autonumber.
OK, now let me see if I understand your code correctly.
The above code is for the Append query correct?
I have already imported the text file into Access and named it, say
'Table B', correct?
I'm trying to understand where in the query it finds 'unmatched'
records?
I don't quite understand WHERE txtfile.LastN IS NULL?

The IS NULL is a sneaky trick - it's called a "Frustrated Outer Join"
query. The LEFT OUTER JOIN by itself returns all records in TableA; if
there is a match in TableB you'll see it, but if there is no match
then all TableB fields will be shown as NULL. The IS NULL criterion
will select just those rows which *don't* have a match.
 
TWO additional tables: a table of Rooms, and a table of
RoomAssignments. RoomAssignments should have (at least) two fields -
the student ID and the room number (or unique room ID).

Yes this makes sense to me now.
A couple of things are bothering me though.
You're suggesting I set it up something like this?:

Student table:
[StudentID] [Lastname] [Firstname]

Rooms table:
[RmID] [RoomNum]

Timetable table:
[StudentID] [Per1RmID] [Per2RmID] [Per3RmID] [Per4RRmID]

Not sure how to combine Per1, Per2 etc. with RmID
Also, I will probably start to make use of the school's
StudentIDNumber system next semester so that I can uniquely identify
every student. They use a nine-digit number for each student.
If I use it, should I do away with my StudentID autonumber system and
use the school's ID number as the Key index field in my Student table?

What I don't like about this though, is that my tables will be
cluttered with much larger numbers (Student ID's 9 digits vs. 2 or 3
digits). Also the creation of these additional tables will necessitate
storing 600+ of these large student IDs in them.
Wouldn't it be more efficient just to throw in the four room numbers
into my Student table as I now have it?
The IS NULL is a sneaky trick - it's called a "Frustrated Outer Join"
query. The LEFT OUTER JOIN by itself returns all records in TableA; if
there is a match in TableB you'll see it, but if there is no match
then all TableB fields will be shown as NULL. The IS NULL criterion
will select just those rows which *don't* have a match.

I created a couple of test tables to try out your code (TestOrig) and
(TestLatest) and ran the following code:

INSERT INTO TestOrig ( LastName, FirstName, HomeForm, Per3 )
SELECT TestLatest.LastName, TestLatest.FirstName, TestLatest.HomeForm,
TestLatest.Per3
FROM TestOrig LEFT JOIN TestLatest ON (TestOrig.LastName =
TestLatest.LastName) AND (TestOrig.FirstName = TestLatest.FirstName)
WHERE (((TestLatest.LastName) Is Null));

But the append query couldn't find any records to append. I made the
tables quite simple. The TestOrig had 10 records. For TestLatest I
added a new record in the number 10 spot and copied the TestOrig's
10th record to number 11 spot.
So the Append query should find the 'new' number 10 record and append
it to TestOrig.
Any ideas why it didn't work?

Thanks! Eric
 
see in-line

TWO additional tables: a table of Rooms, and a table of
RoomAssignments. RoomAssignments should have (at least) two fields -
the student ID and the room number (or unique room ID).

Yes this makes sense to me now.
A couple of things are bothering me though.
You're suggesting I set it up something like this?:

Student table:
[StudentID] [Lastname] [Firstname]

Rooms table:
[RmID] [RoomNum]

Timetable table:
[StudentID] [Per1RmID] [Per2RmID] [Per3RmID] [Per4RRmID]

No - this should be: StudentId, Period, RoomId
Not sure how to combine Per1, Per2 etc. with RmID
Also, I will probably start to make use of the school's
StudentIDNumber system next semester so that I can uniquely identify
every student. They use a nine-digit number for each student.
If I use it, should I do away with my StudentID autonumber system and
use the school's ID number as the Key index field in my Student table?

Yes - I would recommend it.
What I don't like about this though, is that my tables will be
cluttered with much larger numbers (Student ID's 9 digits vs. 2 or 3
digits). Also the creation of these additional tables will necessitate
storing 600+ of these large student IDs in them.
Wouldn't it be more efficient just to throw in the four room numbers
into my Student table as I now have it?

I would not recommend storing the room number(s) in each student record.
Think of it this way, what are you going to do if a new room is added?
 
TWO additional tables: a table of Rooms, and a table of
RoomAssignments. RoomAssignments should have (at least) two fields -
the student ID and the room number (or unique room ID).

Yes this makes sense to me now.
A couple of things are bothering me though.
You're suggesting I set it up something like this?:

Student table:
[StudentID] [Lastname] [Firstname]
Yes...

Rooms table:
[RmID] [RoomNum]
Yes...

Timetable table:
[StudentID] [Per1RmID] [Per2RmID] [Per3RmID] [Per4RRmID]

NO.

Tables should expand DOWN not ACROSS. Timetable would be:

[StudentID] [Period] [RmID]

If a student is assigned to six periods, there would be SIX RECORDS -
not six fields! - in this table for that student.
Not sure how to combine Per1, Per2 etc. with RmID

You don't. You'ld run four Append queries from your wide-flat table,
or one "Normalizing Union Query":

SELECT StudentID, (1) AS Period, Rm1 AS RmID
FROM timetable
WHERE Rm1 IS NOT NULL
UNION ALL
SELECT StudentID, (2) AS Period, Rm2 AS RmID
FROM timetable
WHERE Rm2 IS NOT NULL
UNION ALL
SELECT StudentID, (3) AS Period, Rm3 AS RmID
FROM timetable
WHERE Rm3 IS NOT NULL
UNION ALL
SELECT StudentID, (4) AS Period, Rm4 AS RmID
FROM timetable
WHERE Rm4 IS NOT NULL

and base an Append query on this.


Also, I will probably start to make use of the school's
StudentIDNumber system next semester so that I can uniquely identify
every student. They use a nine-digit number for each student.
If I use it, should I do away with my StudentID autonumber system and
use the school's ID number as the Key index field in my Student table?

Yes. Some will disagree with me (I'm in a discussion on this very
subject in the thread entitled "DB Normalization" in
m.p.a.gettingstarted), but I'd say if you have a unique, stable,
reasonably-sized natural key such as an officially assigned StudentID,
USE IT. You'll want to include it in your table ANYHOW, uniquely
indexed, so you already will *have* the overhead!
What I don't like about this though, is that my tables will be
cluttered with much larger numbers (Student ID's 9 digits vs. 2 or 3
digits). Also the creation of these additional tables will necessitate
storing 600+ of these large student IDs in them.

<g> An Access database can hold 2 GByte of data. If you had 600,000
students you might want to start getting a bit concerned; if you had
six million you'ld be using SQL Server anyway.
Wouldn't it be more efficient just to throw in the four room numbers
into my Student table as I now have it?

IF you can be ABSOLUTELY CERTAIN that you will NEVER in the course of
the user of the database ever need a fifth room. Oh yes... you did say
you needed to add four new rooms already... oh my... <g>

In addition, with the normalized design it's dead easy to find out
"Who's assigned to room 3 anytime today" or "how many students are in
rooms 3 or 4 in the morning" or all sorts of changes - searching only
one field. In your case you need a complex search of four fields.
I created a couple of test tables to try out your code (TestOrig) and
(TestLatest) and ran the following code:

INSERT INTO TestOrig ( LastName, FirstName, HomeForm, Per3 )
SELECT TestLatest.LastName, TestLatest.FirstName, TestLatest.HomeForm,
TestLatest.Per3
FROM TestOrig LEFT JOIN TestLatest ON (TestOrig.LastName =
TestLatest.LastName) AND (TestOrig.FirstName = TestLatest.FirstName)
WHERE (((TestLatest.LastName) Is Null));

But the append query couldn't find any records to append. I made the
tables quite simple. The TestOrig had 10 records. For TestLatest I
added a new record in the number 10 spot and copied the TestOrig's
10th record to number 11 spot.
So the Append query should find the 'new' number 10 record and append
it to TestOrig.
Any ideas why it didn't work?

Because it duplicated the 10th record. That's what the query is
designed to do - to append only records that are NOT duplicates. This
record IS a duplicate.
 
Hi Jeff,

Thanks for your help!
Timetable table:
[StudentID] [Per1RmID] [Per2RmID] [Per3RmID] [Per4RRmID]

No - this should be: StudentId, Period, RoomId

OK, got it now.
Four separate records for each student with four classes.
Yes - I would recommend it.

OK, thanks.

I would not recommend storing the room number(s) in each student record.
Think of it this way, what are you going to do if a new room is added?

I understand the principle of normalized tables and their greater
efficiency overall.
But in situations like mine, which is:
a) not mission-critical data
b) The maximum Rooms a student can have in a day at our school is
four. More rooms will never be added. What will change are the
Rm. nos. when there is a new semester.

Eric
 
HI John,
Timetable table:
[StudentID] [Per1RmID] [Per2RmID] [Per3RmID] [Per4RRmID]

NO.

Tables should expand DOWN not ACROSS. Timetable would be:

[StudentID] [Period] [RmID]

OK, I understand now. Many records per student.

Yes. Some will disagree with me (I'm in a discussion on this very
subject in the thread entitled "DB Normalization" in
m.p.a.gettingstarted), but I'd say if you have a unique, stable,
reasonably-sized natural key such as an officially assigned StudentID,
USE IT. You'll want to include it in your table ANYHOW, uniquely
indexed, so you already will *have* the overhead!

OK. Couldn't I use the school's 9-digit ID in the Students table to
get a unique identifier, but just use it there to save space.
That is, also use an autonumber ID as I now have it for the Students
table and all the other ones?

IF you can be ABSOLUTELY CERTAIN that you will NEVER in the course of
the user of the database ever need a fifth room. Oh yes... you did say
you needed to add four new rooms already... oh my... <g>

Actually four will always be the maximum number of rooms per day.
What will change is the room number for those rooms at a semester
change. And though it may be nice at some point in the future to be
able to pull up all students in Rm. 102 during Per. 3, it is not
required by this database. The school's main databse system (Maplewood
ughhhh!!!) is able to pull up this sort of info quite easily.

Because it duplicated the 10th record. That's what the query is
designed to do - to append only records that are NOT duplicates. This
record IS a duplicate.

I don't follow you. Perhaps you misunderstood me?
My TestOrig has ten separate records with ten different students.
My TestLatest has eleven separate records.

I copied the TestOrig data from its first nine (9) records into
TestLatest. Then I added a completely new student as TestLatest's 10th
record. After I did that, I copied TestOrig's 10th record into
TestLatest's table, which turned out to be its 11th record.
I deliberately wanted a new record to mixed in with the originals.

Then I ran the Append query. But it didn't find the record which ISN'T
a duplicate, the tenth record.

Eric
 
Please excuse me for butting in here, but this situation is similar to a problem I'm having

The way I understand Eric's problem is that he has a flat table listing students with the rooms they are in for each period, something like this

Student Period 1 Room Period 2 Room Period 3 Room ..
1 101 211 10
2 104 211 10
3 ..
and so forth

I understand how to create and normalize the tables, but how do you query the schedule table in a way that retrieves the data in its original flat-file format?
 
I understand how to create and normalize the tables, but how do you query the schedule table in a way that retrieves the data in its original flat-file format?

With a Crosstab query, using the room number as the column header.
 
Thanks much! It took a few tries to get the "value" part to work, but now it works like a charm.
 
Back
Top