Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old June 27th, 2008, 08:25 PM
colin spalding
Guest
 
Posts: n/a
Default Nz Functions in Control Source

Access 2003

I posted this last week; unfortunately, none of the suggested
solutions cured the problem

I have a subform which lists financial transactions for a client in
the main form, which I total in a textbox named "txtTotalPremium" in
the subform footer with the Control Source "=Sum([PremiumGBP])";
without quotations of course. This works fine until the subform has
no records to return, in which case textbox result is blank. That
would be fine except I need to use the result in another text box on
the main form as part of another calculation. I have tried what seems
like a hundred different combinations of the Nz and IIf functions all
to no avail. It seems to me that the straight forward
"=Nz(Sum([PremiumGBP]),0)" should do it, but unfortunately not;
whatever I do I get a blank textbox and "#Error" in the mainform
textbox, Where do I go from here?

As an afterthought. Should it matter that the Subform is on a Tab
Control?

  #2  
Old June 27th, 2008, 08:25 PM
Arch
Guest
 
Posts: n/a
Default Re: Nz Functions in Control Source

On Mon, 2 Jun 2008 06:01:00 -0700 (PDT), colin spalding
<colin.mardell@btopenworld.comwrote:
Quote:
>Access 2003
>
>I posted this last week; unfortunately, none of the suggested
>solutions cured the problem
>
>I have a subform which lists financial transactions for a client in
>the main form, which I total in a textbox named "txtTotalPremium" in
>the subform footer with the Control Source "=Sum([PremiumGBP])";
>without quotations of course. This works fine until the subform has
>no records to return, in which case textbox result is blank. That
>would be fine except I need to use the result in another text box on
>the main form as part of another calculation. I have tried what seems
>like a hundred different combinations of the Nz and IIf functions all
>to no avail. It seems to me that the straight forward
>"=Nz(Sum([PremiumGBP]),0)" should do it, but unfortunately not;
>whatever I do I get a blank textbox and "#Error" in the mainform
>textbox, Where do I go from here?
>
>As an afterthought. Should it matter that the Subform is on a Tab
>Control?
I haven't tried this to confirm, but I wonder if the problem might be
the result of attempting to sum a null. Have you tried

=Sum(Nz([PremiumGBP], 0))

Hope it helps...
  #3  
Old June 27th, 2008, 08:25 PM
lyle fairfield
Guest
 
Posts: n/a
Default Re: Nz Functions in Control Source

I think a standard solution (it may or may not be suitable for your
situation) for this problem is:

Create a query that calculates the aggregate sum of PremiumGBP grouped
on the field linking your sub form.

Left Join to that query in the main form's record-source, or the sub-
form's record-source, whichever is appropriate.

Use that field's value instead of =Sum([PremiumGBP])




On Jun 2, 9:01*am, colin spalding <colin.mard...@btopenworld.com>
wrote:
Quote:
Access 2003
>
I posted this last week; unfortunately, none of the suggested
solutions cured the problem
>
I have a subform which lists financial transactions for a client in
the main form, which I total in a textbox named "txtTotalPremium" in
the subform footer with the Control Source "=Sum([PremiumGBP])";
without quotations of course. *This works fine until the subform has
no records to return, in which case textbox result is blank. *That
would be fine except I need to use the result in another text box on
the main form as part of another calculation. *I have tried what seems
like a hundred different combinations of the Nz and IIf functions all
to no avail. *It seems to me that the straight forward
"=Nz(Sum([PremiumGBP]),0)" should do it, but unfortunately not;
whatever I do I get a blank textbox and "#Error" in the mainform
textbox, *Where do I go from here?
>
As an afterthought. *Should it matter that the Subform is on a Tab
Control?
  #4  
Old June 27th, 2008, 08:25 PM
bobh
Guest
 
Posts: n/a
Default Re: Nz Functions in Control Source

On Jun 2, 9:01*am, colin spalding <colin.mard...@btopenworld.com>
wrote:
Quote:
Access 2003
>
I posted this last week; unfortunately, none of the suggested
solutions cured the problem
>
I have a subform which lists financial transactions for a client in
the main form, which I total in a textbox named "txtTotalPremium" in
the subform footer with the Control Source "=Sum([PremiumGBP])";
without quotations of course. *This works fine until the subform has
no records to return, in which case textbox result is blank. *That
would be fine except I need to use the result in another text box on
the main form as part of another calculation. *I have tried what seems
like a hundred different combinations of the Nz and IIf functions all
to no avail. *It seems to me that the straight forward
"=Nz(Sum([PremiumGBP]),0)" should do it, but unfortunately not;
whatever I do I get a blank textbox and "#Error" in the mainform
textbox, *Where do I go from here?
>
As an afterthought. *Should it matter that the Subform is on a Tab
Control?
a shot in the dark..... have you tried doing a control.refresh or
control.requery of the subform control before the main form calulation
bobh.
  #5  
Old June 27th, 2008, 08:25 PM
Albert D. Kallal
Guest
 
Posts: n/a
Default Re: Nz Functions in Control Source

Your problem is quite common, and most developers encounter your exact case
quite often.

The problem is that when the sub-form has no records, then the control does
not return a number, and then nz() fails. (because it is NOT a null value
being returned).

The solution is to build a custom "nz()" function when you need to reference
a sub-form total value.

I use:

Public Function nzobj(objvar As Variant) As Currency

' used in place of nz for sub-reports, since the value returned is *not*
a number
' when there is no resulting value for the sub form
'

If IsNumeric(objvar) Then
nzobj = objvar
Else
nzobj = 0
End If

End Function


Remember, you don't need/want to use the above function for the sum in the
sub-form. You can leave that as you have:

eg:

=Sum([PremiumGBP])

However, in your "main" form (parent form), you need the above function.

=(nzobj(ResOptions.Report.txtSumOfPremiumGBP))

I can't quite remember, but my spider sense tells me this was more of a
problem when using reports, then forms. However, either way, the above
should solve your issue.


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@msn.com


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles