MS Access Creating a New Order And Orderline

為{幸葍}努か 提交于 2020-01-17 13:38:06

问题


I have a Customer table, an Order table, an Orderline table and a Product table. All of them have an Autonumber field as their primary key, and Orderline has a foreign key reference to Order ID on Order table:

ORDER
-----
Order ID - Autonumber  
Customer ID - Number  
...

ORDERLINE
---------
OrderLine ID - Autonumber  
Order ID - FK to Order  
Product ID - FK  to Product  
Quantity  

PRODUCT
-------
Product ID - Autonumber  
Product details...  

I have a form where I can choose a customer, and then a list of records from the Orderline table, and a query which I reference from this sub-form which lists the Order ID, Orderline ID, Product ID, Product details...

I have 2 problems.

  1. All the orders appear, and I only want the ones associated with this order, (which should be none when the form first loads).

  2. When I enter a Product ID that I want to add to a new order, I am expecting a new Order ID to appear, (Autoincremented) AND a new Orderline ID, (Autoincremented) and the details of the product that I have selected, corresponding to theProduct ID` I have entered, but instead I get this error message:

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again

The thing is, the tables should create me unique keys when I try to create the new record, and when I go into each table directly and enter a new record, the autonumber does work and does create a unique key - it is just when it is trying to create both the Order ID and the Orderline ID at the same time that it seems to be failing.

I should say, I have spent days on this, searched countless search engines, watched whole series of YouTube videos on creating Order forms but to no avail. Anyone who understands Access I am sure would be able to help me, as I would be able to help anyone in a similar circumstance in a matter of minutes if this was a problem in SQL.


回答1:


When you create a sub-form you have to specify the relationship between the parent and sub-form. the same relationship you have created for your tables. Then only Access will filter the records for you.

Regarding your question. You should create a new [order] record in [order] table where you will be entering/selecting [customer_id, staff_id, order details ect]

one order can have more than one items so your [order_items] table (i assume orderline is the term you use for this table) exists of

  • order_id
  • product_id (order_id, product_id composite key)
  • quantity
  • price
  • etc..

Now when you want to start taking order you need to create a new form that is bound to tbl_order. In the frm_order you will have a sub-from which is bound to tbl_oder_items (in your case orderline)

The frm_order and frm_oder_items should be have a relationship. usually when you drag the table to create the subform ACCESS will ask to set the relationship. if you create the subform manually:

  • Select the sub-form
  • go to property sheet
  • select the link master field : order_id
  • select the link child field : order_id

Now when you open the frm_order it will show all the records (in other words all the products the order has in its list) from the tbl_order_items table.

Your tbl_order_item /orderline table also referencing to the product table via the product_id field.

Insert a combobox in the frm_order_items and bound it to the product_id. The combobox's rowsource would be

select product_id, product_name from tbl_product

This error message will occur: 'The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again'

when you try to add a product twice for the same order. instead you should increase the quantity for the product.

Try this and let us know how it went.



来源:https://stackoverflow.com/questions/29800907/ms-access-creating-a-new-order-and-orderline

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