General Question

manuel_alarcon's avatar

What is the right access 2007 sql syntax for adding the last record of another table?

Asked by manuel_alarcon (299points) February 23rd, 2010

Im adding records programatically to several tables in an Access2007 database. I find access sql confusing, I’m used to plain simple ol’ sql dialect. So, I have the “project” table which leads all; the “samples” table and the “analysis” and “result” tables, all depending on a “relations” table, being this the one that takes the id’s from project and samples so they can be related later.
Now, all works very nicely but here’s my problem: I can create the new records in the project table, but i need to take the newly created “id” from here and create it into the “samples” and “relations” table, and I’m really confused with the sql syntax.

Im thinking:

DoCmd.RunSQL “INSERT INTO samples (id_sample) VALUES(SELECT Last(project.Id) AS LastOfId FROM project );”

would that be correct?

Observing members: 0 Composing members: 0

4 Answers

jaytkay's avatar

INSERT INTO samples (id_sample)
SELECT Last(project.Id) AS LastOfId
FROM project

I tested this and it works.

One nice thing about Access is you can use the graphical query designer to test your syntax. The designer includes “SQL View” if you prefer text over the GUI.
It will not run or save your query if errors exist

manuel_alarcon's avatar

Oh! thanks!!!! I was close too! its been years without programming,... i have to keep up with these. thanks again

jaytkay's avatar

Very good, glad it helped. I forgot to mention that the query designer will also write SQL for you. You drag table & fields around, and then switch to SQL View to copy the query text.

manuel_alarcon's avatar

yes, i was expanding the insert to two records and using the query designer and some copy paste it worked pretty cool… thanks!

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther