Hi, I'm using Access to connect to SQL Server and I'm having trouble getting a form working correctly. I want the form to have everything from the Sales table and also the Name field from the Customer table. SalesID is an autonumber since it's the primary key of that table. The CustomerID field doesn't automatically populate with it's unique ID. I would like it so that I can enter in the customer's name and then automatically know what the unique identifier is. I don't want it so every time I enter in a new record I have to go look up that customer's CustomerID and key it in manually. The way it's setup now, it won't insert the record if I don't put in the CustomerID. I'm guessing I designed this incorrectly but I'm not sure what do differently. The way I enter in a new sale now is I just call a stored procedure I wrote which finds the customers name based on the unique ID:
CREATE PROCEDURE New_Sale
@CustomerName varchar(30),
@Product varchar(100),
@Quantity int,
@Date smalldatetime
AS
DECLARE @Cid as int
SELECT @Cid = CustomerID
FROM dbo.CUSTOMER
WHERE [Name] = @CustomerName;
BEGIN
INSERT INTO SALES(CustomerID, Product, Quantity, [Date])
VALUES(@Cid, @Product, @Quantity, @Date);
END
UPDATE dbo.[PRODUCT]
SET [in stock] = ([in stock] - @Quantity)
WHERE @Product = Model;
GO
I'd rather enter the sale into a form instead of running this stored procedure and being prompted for the parameters (users are more prone to keying mistakes). Do I need to alter the sales table in order for the CustomerID to be generated automatically after entering in the customers name? Here is a diagram of what my tables/relationships look like: