How to insert a new field into a table which related to another table?

纵然是瞬间 提交于 2019-12-13 04:22:44

问题


I have two tables in MS Access 2013.

There are some conditions:
1. Part can only be withdrawn after serviced.
2. After certain time, part will be returned for service again.
3. Part can be recycled.

tblService:

(ID is the primary key)

ID  PART_ID SERV_DATE
1    A0001  11/1/2013
2    A0001  11/13/2013
3    A0001  11/25/2013
4    B0001  11/26/2013
5    C0001  12/1/2013
6    C0001  12/10/2013
7    C0001  12/20/2013
8    A0001  12/21/2013

tblWithdraw:

(ID is the primary key)

ID PART_ID DRAWN_DATE DRAWN_REASON DRAWN_TO
1   A0001  11/6/2013       PM         601
2   A0001  11/20/2013    120 PM       603
3   A0001  11/30/2013  REPLACEMENT    605
4   C0001  12/2/2013      30 PM       701
5   C0001  12/15/2013    180 PM       702
6   B0001  12/18/2013      PM         801
7   A0001  12/25/2013     60 PM       502

I'd like to insert a new field, SERVICE_ID, into tblWithdraw, look like this:
ID PART_ID DRAWN_DATE DRAWN_REASON DRAWN_TO SERVICE_ID
1   A0001  11/6/2013       PM         601       1
2   A0001  11/20/2013    120 PM       603       2
3   A0001  11/30/2013  REPLACEMENT    605       3
4   C0001  12/2/2013      30 PM       701       5
5   C0001  12/15/2013    180 PM       702       6
6   B0001  12/18/2013      PM         801       4
7   A0001  12/25/2013     60 PM       502       8

which SERVICE_ID is the ID in tblService.

However, when I tried codes below in the query:

INSERT INTO tblWithdraw ( SERVICE_ID ) SELECT ID  

FROM tblService;

It gave me:

ID PART_ID DRAWN_DATE DRAWN_REASON DRAWN_TO SERVICE_ID
1   A0001  11/6/2013       PM         601
2   A0001  11/20/2013    120 PM       603
3   A0001  11/30/2013  REPLACEMENT    605
4   C0001  12/2/2013      30 PM       701
5   C0001  12/15/2013    180 PM       702
6   B0001  12/18/2013      PM         801
7   A0001  12/25/2013     60 PM       502
8                                               1
9                                               2
10                                              3
11                                              4
12                                              5
13                                              6
14                                              7
15                                              8
Or codes below:
INSERT INTO tblWithdraw (SERVICE_ID) SELECT ID FROM tblService WHERE tblService.PART_ID =tblWithdraw.PART_ID;  

The system can't recognize tblWithdraw.PART_ID.

Can anyone correct my codes or give me answer or direct me to the solution? Thanks!


回答1:


As you have discovered, INSERT statements always add new rows to a table. You want to UPDATE existing rows using something like this:

UPDATE tblWithdraw
SET SERVICE_ID = DLookup("ID","tblService","PART_ID='" & PART_ID & "' AND SERV_DATE=#" & Format(DMax("SERV_DATE","tblService","PART_ID='" & PART_ID & "' AND SERV_DATE<=#" & Format(DRAWN_DATE,"yyyy-mm-dd") & "#"),"yyyy-mm-dd") & "#")

It uses DMax() to find the most recent SERV_DATE that precedes a given DRAWN_DATE, then does a DLookup() to find the corresponding [tblService].[ID], and then plugs it into the SERVICE_ID column in [tblWithdraw].



来源:https://stackoverflow.com/questions/20309834/how-to-insert-a-new-field-into-a-table-which-related-to-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!