Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement subform/mainform unbound not working 1

Status
Not open for further replies.

naiku

Technical User
Apr 25, 2002
346
US
I have a total field on a subform, calculated by summing 2 fields on the main form. My subform is divided into 2 sections (Primary and Secondary) the user selects whether it is a primary or secondary option on the main form by clicking on a checkbox (which then updates a field on the subform "txtSecondary" with an ID, which if the checkbox is not checked is blank).

Depending on this choice, I want the total to either appear in the Primary total, or the Secondary total on my subform. This is where I begin to run into problems, and I can't figure out why. My total is based on 2 unbound text boxes on the main form, these 2 text boxes are completed using a calculation from 2 further subforms (I have 3, the actual calculation is on the subforms, and the unbound text boxes take their data from the calculation result). If I go onto my totals subform and in the design of field txtPrimaryTotal enter the control source as =[Forms]![frmJobHeader]![txtTotalA]+[Forms]![frmJobHeader]![txtTotalB] then I get the correct total. However I want this total to either appear txtPrimaryTotal or txtSecondaryTotal depending on the outcome of a check box on the main form.

I tried putting on the after update event of the checkbox on the main form:

If chkSecondary = true then
Me!frmTotals![txtSecondaryTotal] = [Forms]![frmJobHeader]![txtTotalA]+[Forms]![frmJobHeader]![txtTotalB]
Me!frmTotals![txtPrimaryTotal] = 0
Else
Me!frmTotals![txtPrimaryTotal] = [Forms]![frmJobHeader]![txtTotalA]+[Forms]![frmJobHeader]![txtTotalB]
Me!frmTotals![txtSecondaryTotal] = 0
End If


But this does not work, I also tried changing (and moving) the If statement from the Main form onto the Totals subform's on current event, and had it as If txtSecondary = " " Then follow the statements as above. But this also does not work, no matter where I put the If statement, it would not populate the totals fields on the subform (however it did always change the Primary or Secondary total to 0 depending on the outcome). The more I think about it, the more I think it has something to do with the unbound controls on the main form. They are populated as:

Subform > Calculated Unbound Control > MainForm > Unbound Control = Calculated Unbound Control on Subform.

Should I scrap the 2 unbound controls on the main form, and in my If statement sum the 2 unbound controls on the subforms? So that it looks something like Me!frmTotals![txtSecondaryTotal] = [Forms]![frmSubform1]![txtUnboundControl1]+[Forms]![frmSubForm2]![txtUnboundControl2] The only thing I am then worried about is can I reference information on a subform from another subform? Sorry for the long post. Thanks.
 
No, not too familiar with iif.
 
Okay I think I have an idea of the Iif function, for the controls should I have the control source for txtPrimaryTotal as:

= IIf ([Forms]![frmTotals]![txtSecondary] = " ", [Forms]![frmJobHeader]![txtTotalA]+[Forms]![frmJobHeader]![txtTotalB], 0)

And for txtSecondaryTotal set the control source to:

= IIf ([Forms]![frmTotals]![txtSecondary] = " ", 0, [Forms]![frmJobHeader]![txtTotalA]+[Forms]![frmJobHeader]![txtTotalB])
 
Woohoo learned something new today and it works a treat. Thanks lordfidan, I looked up iif and entered the control source of my fields as:

= IIf ([txtSecondary] = " ", ([Forms]![frmJobHeader]![txtTotalA]+[Forms]![frmJobHeader]![txtTotalB]) ,0)

and just switched the arguments around for the txtSecondaryTotal.

Thanks again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top