Solved

ms access #TYPE! error on report when no data

Posted on 2016-08-26
4
51 Views
Last Modified: 2016-09-07
Hello-

 I have an MS access report. Run perfect when where is data.   If there is NO data I get a #Type!  error on a particular field

In the  report itself, under the detail section I have a text box that contains a  TRIM statement:   =Trim([PRIMARY_NAME]) & " " & Trim([Label_NAME])

If there is no data on the report, I get #Type!  error showing on the report when I run the print preview.

I tried to modify the trim statement to be :   =IIf(IsNull([PRIMARY_NAME])," ",Trim([PRIMARY_NAME]) & " " & Trim([Label_NAME]))       but that did not work.

I wrote it so if the PRIMARY_NAME field was null then it would just output a blank space. This was probably wrong because there is 2 fields in my trim statement.


I am probably missing something simple....  can someone give advice how to get rid of that #Type! error?  Thank you.
0
Comment
Question by:joylene6
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Nick67
ID: 41772553
IsNull vs Nz()
IsNull returns a Boolean, true or false
Nz(TheNullItem, TheSubstitute) substitutes something for the null.

Why are you TRIMing?
You should take care of that earlier.
Here's a fun trick.
In the query create two computed fields
tPRIMARY_NAME:Trim(PRIMARY_NAME)
tLabel_NAME: Trim(LABEL_NAME)

Make your control
=[tPRIMARY_NAME] + " " + [tLabel_NAME]

Only in Access controls does the + concatenation propogate null.
You'll only get the text of the Primary name, the text of the Label name, or both with a space in between.
0
 
LVL 1

Author Comment

by:joylene6
ID: 41772585
I was/am concatenating  the 2 fields.  I could put them together in the query.  
Just wondering if there was a way to do this in the report.
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41772873
The problem with an empty recordset is that you don't even get a NULL, but #Error.

Paste the following code into a standard module:

Function AvoidError(n As Variant, varReplaceWith As Variant)

10      On Error GoTo AvoidError_Error

20      AvoidError = Nz(n, varReplaceWith)

AvoidError_Exit:
30      Exit Function

AvoidError_Error:
40      AvoidError = varReplaceWith
50      Resume AvoidError_Exit

End Function

 and make sure you call the module something other then AvoidError if it's a new module.  Then make your control source:

=AvoidError(Trim([PRIMARY_NAME]) & " " & Trim([Label_NAME]),"")

This doesn't always work in all cases though.  Microsoft built in some optimizations that defeat this in some cases.  So sometimes you need to break it up in to two steps; A hidden control with your expression, then a visible control with =AvoidError(<visible control>).

The other way to do this is to either check the recordcount in your IIF statement or hasdata and if no records, display a blank (this is looking at a subreport):

IIf(me.rptMonthlyStatCo.report.HasData, Trim([PRIMARY_NAME]) & " " & Trim([Label_NAME]),"")

I find AvoidError() easier to use and it works most of the time without issue.

Jim.
0
 
LVL 1

Author Closing Comment

by:joylene6
ID: 41788343
Thank you for explaining this to me. It did happen to work.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now