Solved

# Default value in cross tab report based on For statement

Posted on 2013-06-05
349 Views
Hi, I have vba statement that adds an OT row to a cross tab report in access. The statement looks like this:
For intX = 2 To intColumnCount
Me("sTot" + Format(intX)) = lngRgColumnTotal(intX) - 40
Next intX

This has both over and undertime answers. What I would like to do is only show the overtime results and set the negative results to 0 on the report. I have tried to use an if statement but I have been unsuccessful.

Thanks for the help.
0
Question by:smolbeck367
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3

LVL 47

Accepted Solution

Dale Fye (Access MVP) earned 500 total points
ID: 39223074
Me("sTot" + Format(intX)) = IIF(lngRgColumnTotal(intX) - 40 < 0, 0, lngRgColumnTotal(intX)-40)
0

LVL 47

Expert Comment

ID: 39223085
Another method would be to use the following function:
``````Public Function fnMax(ParamArray ValList() As Variant) As Variant

Dim intLoop As Integer
Dim myVal As Variant

For intLoop = LBound(ValList) To UBound(ValList)
If Not IsNull(ValList(intLoop)) Then
If IsEmpty(myVal) Then
myVal = ValList(intLoop)
ElseIf ValList(intLoop) > myVal Then
myVal = ValList(intLoop)
End If
End If
Next
fnMax = myVal

End Function
``````
Copy the function and save it in a standard code module, then use the following expression in your loop:

Me("sTot" + Format(intX)) = fnMax(0, lngRgColumnTotal(intX) - 40)

fnMax will accept an array of numbers, strings, or dates and find the maximum value in that array.
0

LVL 29

Expert Comment

ID: 39223095
``````Dim lngTotal As Long
For intX = 2 To intColumnCount
lngTotal = lngRgColumnTotal(intX) - 40
If lngTotal < 0 Then lngTotal = 0
Me("sTot" + Format(intX)) = lngTotal
Next intX
``````
0

Author Closing Comment

ID: 39223323
fyed - your first post worked beautifully. Thank for your quick and very useful response.
0

LVL 47

Expert Comment

ID: 39223433
Glad to help.  I generally prefer to use the function, as I can pass as many arguments as I want, and they can be text or date as well.  I have a similar fnMin() function, both are part of my library module that I install in almost every database I create.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
###### Suggested Courses
Course of the Month9 days, 6 hours left to enroll