The
ResyncCommand property allows you to write an SQL statement that Microsoft SQL Server executes to fix up data from the primary table whenever the user modifies a value in a foreign key field and saves the record. The SQL statement must select the same fields from the primary table that are included in the stored procedure.
The SQL statement must also contain a WHERE clause that passes
? as a parameter marker for the primary key column from the primary table in the stored procedure. This is necessary so that Microsoft SQL Server can resync the form to the same record as the one modified in Microsoft Access.
A general rule is that you should set the
ResyncCommand property to the same SQL statement as the stored procedure being used as the record source, minus the WHERE clause being used in the stored procedure. Then add a WHERE clause to the SQL statement that parameterizes the primary key columns from the table designated in the
UniqueTable property of the form.
Creating a Form That Duplicates the AutoLookup Behavior
- Open an Access project connected to the sample NorthwindCS database on MSDE or Microsoft SQL Server.
- Create and save the following stored procedure:
CREATE PROCEDURE AutoLookUpTest
As
SELECT
Customers.CompanyName,
Customers.ContactName,
Customers.Address,
Orders.*
FROM
Customers INNER JOIN ORDERS
ON
Customers.CustomerID = Orders.CustomerID
WHERE
Orders.CustomerID LIKE 'a%'
- In the Stored Procedures list, click AutoLookupTest, and then click AutoForm on the Insert menu.
- Save the form as AutoLookUpTest.
- Open the form in Design view, and set the UniqueTable property of the form to Orders.
- Open the form in Form view, and change the value in the CustomerID field to "ALFKI", "AROUT", or "ANTON" (Modify the value so that it is different from what is currently displayed).
- Move to the next record in your form, and then move back to the record that you just modified. Note that the Company Name, Contact Name, and Address text boxes still display information for the CustomerID that you changed, not the information for the new CustomerID that you entered.
- Open the form again in Design view, and set the
ResyncCommand property as follows:
SELECT Customers.CompanyName,Customers.ContactName, Customers.Address,
Orders.* FROM Customers INNER JOIN ORDERS ON Customers.CustomerID =
Orders.CustomerID WHERE Orders.OrderID = ?
- Save the form, and then open it in Form view.
- Change the value in the CustomerID field to "ALFKI", "AROUT", or "ANTON" (Modify the value so that it is different from what is currently displayed).
- Move to the next record in your form (you are prompted with a message box), and then move back to the record that you just modified. Note that the Company Name, Contact Name, and Address text boxes now display information related to the CustomerID that you just entered.
Multiple-Field Primary Keys
If the primary key of the "foreign" or "many side" table is made up of multiple fields, you must use the
AND clause and parameterize each field within the WHERE clause of the
ResyncCommand property. For example, if the foreign table contains a three-field primary key, the SQL statement for the
ResyncCommand property would look similar to the following:
SELECT
Table1.Field1,
Table1.Field2,
Table1.Field3,
Table2.*
FROM
Table1 INNER JOIN Table2
ON
Table1.ID = Table2.ForeignID
WHERE
Table2.PrimaryKeyField1 = ? AND
Table2.PrimaryKeyField2 = ? AND
Table2.PrimaryKeyField3 = ?