简体   繁体   中英

Trying to take the values from the primary key column of a table and enter it in the foreign key column of another table

So I have four tables and they are linked by primary and foreign keys. I have a form that is used to enter information and I have set my primary keys column set to autofill in Access database. How can I take those values from the autofill column and enter it in the other table that I want. This is my Insert code..

 sqlString = "INSERT INTO [Members] ([MemberName], [Child], [Adult], [PaymentMethod]) VALUES ('" + MemberName + "','" + Child + "','" + Adult + "','" + PaymentMethod + "')"
    cmd = New OleDbCommand(sqlString, myConn)
    cmd.ExecuteNonQuery()

That is what I use to input the info from my form and it works. Now there is another column that nothing is being entered in because it is the foreign key column and my primary key column in the other table is set to autofill with numbers. How can I fill my foreign key column to match my primary key column in the other table? I hope I explained this well, thanks for the help.

You have to manually map the foreign key column.

The foreign key constraint will prevent you for entering anything other than the values in the primary key column.

http://msdn.microsoft.com/en-us/library/ms175464%28v=sql.105%29.aspx

You can fetch the value from the other table corresponding to this column to get the auto-generated primary key value and insert it in this table.

You can also refer this: Foreign Key Not Populating with Primary Key Values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM