Macro to update a field

B

Brian

I have two tables

Product Table contains: ProdID, QuantityInStock

Orders Table: OrderID, ProdID, Date, QuantityOrdered.

I need to run a macro that will calculate the
QuantityInStock after a new quantity is placed in the
orders table.

I will be using a form to enter the values and want the
macro to run after the 'Quantity' field in the form has
been entered.

Any ideas?
 
N

Nikos Yannacopoulos

Brian,

Make a new query on the Product table, and while in the design view, change
it to an Update query (Query > Update query).
In the criteria line below field ProdID enter a criterion like"
Forms![Order Entry Form Name]![ProdID Control Name] (change names
accordingly)
In the Update to like below the QuantityInStock field, enter an expression
like:
[QuantityInStock] - Forms![Order Entry Form Name]![Quantity Control
Name] (change names accordingly)
Save the query.

Now, make a macro as follows:
action: SetWarnings argument: false
action: OpenQuery argument: query name (the one above), rets arguments
default
action: SetWarnings argument: true
Save the macro

The last thing to do is determine the event to run the query. You could use
the Before Update or On Change Event of the Quantity control on the box, but
this will run the action query and subtract stock with EVERY change, so if
the user makes a typing error and then corrects, both will be subtracted!
Therefore I would suggest a separate command button to run the macro
(confirmation that the quantity is correct / final).

This is a workable solution along the lines of what you asked, just because
you mentioned "macro". This has a major disadvantage: press "save" twice and
you deduct the stock on hand twice!
Personally, I would try to add some more functionality to "know" if order is
already saved, so if there is a change (second save) add the old value and
then subtract the new one etc. Regrettably, I don't know if I could do this
just with plain macros; I would definitely go the VB code way.

HTH,
Nikos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top