Solved

Default value in cross tab report based on For statement

Posted on 2013-06-05
5
350 Views
Last Modified: 2013-06-05
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
Comment
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
  • Learn & ask questions
  • 3
5 Comments
 
LVL 48

Accepted Solution

by:
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 48

Expert Comment

by:Dale Fye (Access MVP)
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

Open in new window

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

by:IrogSinta
ID: 39223095
How about this:
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 

Open in new window

0
 

Author Closing Comment

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

Expert Comment

by:Dale Fye (Access MVP)
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

695 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