Data from Form to Subform to Table

T

tsquared1518

I have a main form for material inspection, and another form for the
inpsection results, and tables for each that store the data. For example, you
have to enter the PO# and P/N on the main form, as well as some other info,
then you click to open up the inspection results form in datasheet view. The
inpsection results form shows the PO# and P/N that you entered on the main
form, but it wont store the info in the table for the inspection results. Can
someone please help me figure out how to get the info to store in the
inspection results table as well as the material inspection table? Thanks for
your help!
 
G

golfinray

That is not the way to do it. You don't want to store data twice as that
violates the rules of database normalization. Instead, make sure you put a
key in the inspection results table to match the material inspection table.
This could be the po# or even an autonumber to make those match. Then go to
tools relationships and establish a relationship between the two tables. I
sounds like it should be a one-to-many. Then your data will get saved
properly and your form will communicate to your subform properly and all you
will have to do to display the data is put the two tables together in a query
and build a form from it.
 
T

tsquared1518

Thanks for the responses, and sorry for the confusion in the subject of my
post. I plan on making the second form a subform on my mainform, but I am
having trouble setting the primary keys. One PO can have multiple orders,
with multiple and duplicate P/Ns, and I also run into the problem with the
P/Ns, because they can be made of different materials. An example of my
problem. PO 1234 has DR3-Neoprene and DR3-EPDM, with 2 boxes of the Neoprene
and 4 boxes of the EPDM. I have to inspect each box, and record the
measurements I take in the table (subform Inspection Results). To fix this, I
am making a table with all the P/Ns that shows the P/N, material, durometer,
and vendor. I figured out I will have to use Auto# since there are duplicate
P/N's. If that solves the primary key problem, I should be able to store all
the data from my mainform and subform on one table. I think I am on the right
track. Any thoughts/suggestions?

KenSheridan via AccessMonster.com said:
You've confused the issue a little here by referring to a subform. From your
description it sounds as though you are in fact referring to a separate form
as you mention 'opening' it. A subform is one which is embedded in the main
form and therefore opens with it. Normally the subform is linked to the
parent form on the keys, so the relevant value(s) from the parent form are
automatically inserted into the foreign key column(s) when a new record is
inserted in the subform. You might well find it better to embed your second
form in the first as a subform, which would avid the need to open the second
form and would automatically insert the key value(s).

How you'd link the parent form and subform would depend on what are the keys.
As Milton has explained you must not duplicate non-key values in the
inspection results table. If the primary key of the inspections table is a
composite one of the two columns PO# and P/N the it is quite correct to have
the same two columns as a composite foreign key in the inspection results
table. In this case the parent form and subform would be linked on the two
columns by making the subform control's LinkMasterFields and LinkChildFields
properties [PO#]/[P/N] in each case. If the key is the one column PO#,
however, then the P/N column should not be present in the inspection results
table, only the PO# as a foreign key, in which case the LinkMasterFields and
LinkChildFields properties would be [PO#] in each case. The alternative is,
as Milton suggests, to use an autonumber column as the surrogate primary key
of the inspections table, InspectionID say, and have a long integer number
column of the same name in the inspection results table. This column would
then be used as the LinkMasterFields and LinkChildFields properties.

Using two separate forms, the first question is do you want them linked, i.e.
do you want the inspection results form to show the results just for the
inspection form's current record, or the results of all inspections? If the
former filter the inspection results form when opening it from the
inspections form like so ( I'll assume the use of surrogate InspectionID keys
for this example)

Dim stCriteria As String

' first ensure current inspection record is saved
Me.Dirty = False

strCriteria = "InspectionID = " & Me.InspectionID

' open form filtered to current inspection
DoCmd.OpenForm "frmInspectionResults", WhereCondition:=strCriteria
' set default value of InspectionID control on inspection
' results form to current inspection record's value
Forms("frmInspectionResults").InspectionID.DefaultValue = """" & _
Me.LastName & """"

Note that the DefaultValue property is a string expression regardless of the
data type of the underlying field, so should be wrapped in quotes characters
as above.

If you want it opened to show all inspection results, don't filter it, so
simply use:

' open form unfiltered
DoCmd.OpenForm "frmInspectionResults"
' set default value of InspectionID control on inspection
' results form to current inspection record's value
Forms("frmInspectionResults").InspectionID.DefaultValue = """" & _
Me.InspectionID & """"

Finally, to keep the two form's in sync; if the second form is openbed
filtered put the following in the first form's Current event procedure:

Const FORM_NOT_OPEN = 2450

On Error Resume Next
Forms("frmInspectionResults").Filter = "InspectionID = " & Me.InspectionID
Forms("frmInspectionResults").InspectionID.DefaultValue = """" & _
Me.InspectionID & """"
Select Case Err.Number
Case 0
' no error
Case FORM_NOT_OPEN
' anicipated error so do nothing
Case Else
'unknown error so inform user
MsgBox Err.Description, vbExclamation, "Error"
End Select

If its opened unfiltered put the following:

Const FORM_NOT_OPEN = 2450

On Error Resume Next
Forms("frmInspectionResults").InspectionID.DefaultValue = """" & _
Me.InspectionID & """"
Select Case Err.Number
Case 0
' no error
Case FORM_NOT_OPEN
' anicipated error so do nothing
Case Else
'unknown error so inform user
MsgBox Err.Description, vbExclamation, "Error"
End Select

The error handling is needed in each case to cover the situation where the
record pointer in the first form moves while the second form is not open.

Ken Sheridan
Stafford, England
I have a main form for material inspection, and another form for the
inpsection results, and tables for each that store the data. For example, you
have to enter the PO# and P/N on the main form, as well as some other info,
then you click to open up the inspection results form in datasheet view. The
inpsection results form shows the PO# and P/N that you entered on the main
form, but it wont store the info in the table for the inspection results. Can
someone please help me figure out how to get the info to store in the
inspection results table as well as the material inspection table? Thanks for
your help!

--



.
 

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