John,
There are probably a number of different approaches you could adopt. Here's
one solution that produces what you're after. As the explanation is worse
than the doing of it, here is a step-by-step procedure.
The problem stems from the fact that you are mixing text data ("NA") with
numeric data.
1. Change the query so that it returns only numeric data or Null (meaning
no valid data). Do this by replacing the expression in the query to
something like:
CalcField: IIf([Data2]=0,Null,Round([Data1]/[Data2],3))
The Round() function is used to round the result to three decimal places
after the decimal point. This will give one decimal place after the number
is converted to the Percent format.
2. Still in query design view, right-click the CalcField and select
Properties from the right-click menu. In the Properties dialog, on the
General Tab, click in the Format property and select "Percent". In the
Decimal Places property, select 1. Save the query and run it. For valid
data , you should see percentages rounded to 1 decimal place and, for
invalid data, you should see blanks.
3. Open the Report in design view. The current Textboxes now display the
information incorrectly, but you need to leave them on the report so that
Access can get to the data in those fields in the Report's recordsource.
Make these Textboxes invisible (by changing their Visible property to No).
Create new unbound Textboxes for each calculated field and give each Textbox
a suitable name; eg for the [CalcField] field above, create a Textbox named
"txtCalcField". Set these unbound Textboxes to Percent format and one
decimal place.
4. Double-click the gray bar across the top of the Detail Section to open
the Properties dialog for the Detail Section. Click the Event tab and click
in the Format Property. In the Format Property, select [Event Procedure].
To the right of the Format property, click its Build button (the square
tablet with the elipsis ...). The Visual Basic editor opens and you are
taken to the Format event procedure for the Detail Section of the report.
This format event procedure runs for each record in the data that's being
printed. It's here where you have complete control over the formatting of
the data. In the following example, [CalcField] is the name of the field
calculated by the query. "Me" simply refers to the report. "txtTextBox" is
the name of the unbound Textbox created to receive the data. Your query now
returns Null for invalid data, so we can use the IsNull() function to test
for invalid data.
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
' Do first calculated field:
If IsNull([CalcField]) Then
Me.txtTextBox = "N/A"
Else
Me.txtTextBox = [CalcField]
End If
' Repeat similar coding for every calculated field.
End Sub
5. After you've written the code, you can switch back to Access (eg by
using the Access button on the Taskbar at the bottom of your screen). Run
the report to see if it's working OK. The button for the VBA editor will
still be on the Taskbar if you need to switch back to the VBA editor to
adjust the code. Alternatively, you can repeat the above steps to get to
the Format event procedure for the Detail Section. When finished coding,
close the VBA editor.
If "N/A" wasn't so important, ie if blanks would be acceptable, you needn't
go to the trouble of writing code. However, if "N/A" is important, then
this is one way of getting Access to do it, while displaying the numeric
data exactly as you want it.
I hope this makes sense.
Geoff
John S. Ford said:
I am creating a report that uses a query as a record source. A series of
TextBoxes on the report are bound to some of the query's fields that are
themselves calculated percentages. These fields i.e. Percent1, Percent2,
etc. use IIf conditional statements such that they return a proportion if
the denominator of the calculation is nonzero. It returns "NA" (Not
Applicable) if the denominator is zero thus avoiding a divide-by-zero
problem.
The query works perfectly returning either the proportion or a "NA". The
problem is, I want the report's TextBoxes to format these numbers as
percentages with one decimal point. I have the TextBoxes' Format property
set to "Percent" with the Decimal Point Property set to 1.
When I print the report, it seems to ignore the TextBox Format and Decimal
Point properties and prints the values as simple proportions with endless
decimal points. Any idea why my TextBoxes are behaving this way? The
data
presented by the TextBoxes is correct just not formatted the way I want.
Does this have something to do with the fact that one of the possible
outcomes of the query is the string "NA" and not a number?
Thanks in advance!
John