access repot/form

S

SolarComputing

I need to make a form AND a report showing price times quantity where the
price is determined by the date of the entry. Prices change often and I need
to select the correct price for the entry as well as print past entries.
 
J

james hall

SolarComputing said:
I need to make a form AND a report showing price times quantity where the
price is determined by the date of the entry. Prices change often and I
need
to select the correct price for the entry as well as print past entries.
 
J

John W. Vinson

I need to make a form AND a report showing price times quantity where the
price is determined by the date of the entry. Prices change often and I need
to select the correct price for the entry as well as print past entries.

Well... what problem are you having?

How is your table structured? Where are you storing the price - in the items
table *AND* in the order-details table (or its equivalent); that's what I'd
recommend?
 
T

Tom Wickerath

The Northwind sample database that ships with Access includes saving the
current product price in the Products table, and the price at the time of
sale in the Order Details table. Depending on your version of Access, the
table and field names are slightly different:

Access 2003 and all prior versions:
Products table and Order Details table. The field is named UnitPrice in both
tables.

Access 2007
Products table / List Price field
Order Details table / Unit Price field
Purchase Order Details table / Unit Cost field

To be honest, I haven't spent any time studying the schema of the Northwind
2007 structure, so I'm not entirely certain why they are storing Unit Price
and Unit Cost in the two child (many side) tables.

In any case, the price shown in the Products table represents the current
price. The other price fields represent the price at the time of sale. When
you create a new order, these two prices should always be identical. But, you
can now later update the price in the Products table, without affecting the
total sales amount for past orders.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
S

SolarComputing

I'll take a closer look at Northwind.
Looks like I am using 2002 access in which I made a table with product
details (excluding price) and a table with product prices (which is a history
of price changes for each product). From the "Prices" table I would like to
select the one corresponding with order date. But, how does Northwind make
the link of to "orders detail" table?
 
S

SolarComputing

I have two tables: table "items" with details on items I sell, second table
called "prices" which has the historical prices of each item. I want a
report and form to show me the appropriate price corresponding to the date of
entry?
 
J

John W. Vinson

I have two tables: table "items" with details on items I sell, second table
called "prices" which has the historical prices of each item. I want a
report and form to show me the appropriate price corresponding to the date of
entry?

If the historical prices table isn't too big, you can use a query with a "non
equi join" comparing the date of sale with the date of price change, and
returning the price as of the last change prior to the sale date.

This is the normalized way to do it; Northwind and some other databases
intentionally denormalize a bit, by storing the price in the OrderDetails
record, using code on the data entry form to capture the current price from
the items table directly into the sales record. This has the added advantage
(and disadvantage, depending on your point of view!) that the price-as-sold
can be edited; for instance you might want to give a discounted price to a
specific customer, or for a specific transaction.
 
T

Tom Wickerath

From your reply to John Vinson:
I have two tables: table "items" with details on items I sell, second table
called "prices" which has the historical prices of each item. I want a
report and form to show me the appropriate price corresponding to the date of
entry?

While you can implement a design that stores historical prices for each
item, in order to later produce a report that shows price vs. time for a
given item, I think such a design would be a lot more difficult to pair up
with a given Order Detail record. So, this historical information, if you
choose to keep it, would be an additional table, but not a replacement for a
more standard many-to-many (M:N) relationship that involves three tables:

Products
Order Details
Orders

where UnitPrice is stored in the Products and Order Details tables. The
many-to-many relationship is made up of two one-to-many (1:M) relationships,
where the Order Details table has the foreign key of both relationships. In
words, you have the following:

1.) An order can include zero to many products and
2.) A product can be found in zero to many orders

But, how does Northwind make the link of to "orders detail" table?

The Orders form includes an Orders Subform. The Northwind sample for Access
2003 and prior versions utilizes an AfterUpdate event procedure (ie. VBA
code) for the combo box on the Orders Subform named Products. It includes use
of a Domain Aggregrate function, DLookup, to assign a "lookup up" price to
the UnitPrice control that is bound to the UnitPrice field in the Order
Details table. Here is the associated code that runs, when a person selects a
product:

Private Sub ProductID_AfterUpdate()
On Error GoTo Err_ProductID_AfterUpdate

Dim strFilter As String

' Evaluate filter before it's passed to DLookup function.
strFilter = "ProductID = " & Me!ProductID

' Look up product's unit price and assign it to UnitPrice control.
Me!UnitPrice = DLookup("UnitPrice", "Products", strFilter)

Exit_ProductID_AfterUpdate:
Exit Sub

Err_ProductID_AfterUpdate:
MsgBox Err.Description
Resume Exit_ProductID_AfterUpdate

End Sub

Frankly, an easier (and more efficient method, IMHO) would be to include the
Products table UnitPrice (the current price) in the Row Source for the
Products combo box. This way, you don't need to call the DLookup function at
all. Something like this:

Original RowSource is a SQL (Structured Query Language) statement. You can
leave it as a SQL statement, or save it as a saved query. In either case, it
is still considered a query:

SELECT DISTINCT [ProductID], [ProductName], [Discontinued]
FROM Products
ORDER BY [ProductName];

Revised Rowsource
Note: You shouldn't need the DISTINCT keyword. Also, the square brackets are
required only if your field names include spaces or other special characters
such as a hyphen, or reserved words:

SELECT ProductID, ProductName, Discontinued, UnitPrice
FROM Products
ORDER BY Products.ProductName;

Increase Column Count from 3 to 4
Modify column widths: 0";2.2";1" ---> 0";2.2";1";0" (or 0.5" to display
price)

Change the AfterUpdate procedure for the ProductID combo box to this:

Private Sub ProductID_AfterUpdate()
On Error GoTo ProcError

Me.UnitPrice = Me.ProductID.Column(3)

ExitProc:
Exit Sub
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure ProductID_AfterUpdate..."
Resume ExitProc
End Sub

Notes:

In VBA code, the columns of a combo box are "zero-based". Thus, column 0 is
the first column in the row source, column 1 is the second column in the row
source, etc. This is why I specified column(3), above, since the new
UnitPrice field that I added was added as the fourth field to the rowsource
for the combo box.

I recommend using the dot notation, with the period, instead of the bang
notation (!), when you have a control on the form with the referenced name
(UnitPrice control).

Do a Debug | Compile {ProjectName} after making changes to VBA code. You
want to ensure that you do not have any compile-time errors.

~~~~~~~~~~~~~~~~~~
I recommend that you spend some time reading up on database design. Here is
a good link to start with:

http://www.accessmvp.com/JConrad/accessjunkie/resources.html#DatabaseDesign101

Make sure to read the first two papers by Michael Hernandez. He is the
author of the book "Database Design for Mere Mortals", and is considered an
expert in this field.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
T

Tom Wickerath

Hi John,
Northwind and some other databases intentionally denormalize a bit,
by storing the price in the OrderDetails record, ...

I would not classify this as denormalization. Yes, the UnitPrice is stored
in two different tables, but they do not represent the same attribute. As you
know, one is the current selling price, and the other is the price at the
time of sale.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
S

SolarComputing

Good; I think I am getting somewhere. But I don't understand what a "non
equl join" means? Can I go to Query wizard and bring up the two tables and
do a comparison somehow with "less than or equal to" comparison on the dates?
 
A

Armen Stein

I would not classify this as denormalization. Yes, the UnitPrice is stored
in two different tables, but they do not represent the same attribute. As you
know, one is the current selling price, and the other is the price at the
time of sale.

Nor would I. The type of denormalization being referred to is when
the *same* data is stored in more than one place. The price of
something *at the time* is not the same data as the current or default
price. The temporal aspect of the data must be considered, as must
the possibility of it being overridden for some other reason, so it is
not denormalized in that case.

A more extreme example would be an audit trail showing changes to
values over time. They are snapshots of the same data field at
different moments, so they are still normalized.

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
J

John W. Vinson

Good; I think I am getting somewhere. But I don't understand what a "non
equl join" means? Can I go to Query wizard and bring up the two tables and
do a comparison somehow with "less than or equal to" comparison on the dates?

No; you'll need to go to SQL view to do this. If you could post the relevant
fieldnames of your tables I could probably get you on the right track.

I'd really recommend storing the price-as-of-date in the table though. It's
easier, much simpler query, and more efficient.
 

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