views:

311

answers:

4

Have searched for this, no luck.

Can someone tell me how create a Ceiling Function in MS access that behaves the same as the one in excel?

+4  A: 

Looks like there's an answer here (VBA for Access):

http://www.tek-tips.com/faqs.cfm?fid=5031

Public Function Ceiling(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
    ' X is the value you want to round
    ' is the multiple to which you want to round
    Ceiling = (Int(X / Factor) - (X / Factor - Int(X / Factor) > 0)) * Factor
End Function
Arthur Frankel
I would strongly recommend breaking this down into multiple steps, storing the result of each sub-calculation in a variable with known precision in order to minimize the effects of floating point imprecision.
David-W-Fenton
+1  A: 

You can add a Reference to the Microsoft Excel Object Library and use Excel.WorksheetFunction.Ceiling

marg
Do I have to make a VBA function that wraps the call to Excel.WorksheetFunction.Ceiling() or can I use that in an expression?
Ronnie Overby
You can use it directly in VBA but need to create a wrapper to use the function in a Access Form.
marg
+2  A: 

Thanks, marg, for the answer. For future reference, here is the VBA function that I wrote after importing the Microsoft Excel Object Library:

Public Function Ceiling(Value As Double, Significance As Double) As Double
    Ceiling = Excel.WorksheetFunction.Ceiling(Value, Significance)
End Function

Then in my query, I was trying to calculate billable hours from actual time worked, rounding up to the next quarter hour:

SELECT Ceiling(([WorkTimes]![EndTime]-[WorkTimes]![BeginTime])*24,0.25) AS BillableTime
FROM WorkTimes;
Ronnie Overby
A: 

While this question specifically asked for Access here is the answer for VB.NET

Public Function Ceiling(ByVal value As Double, ByVal factor As Double) As Double
    Return Math.Ceiling(value / factor) * factor
End Function

And the answer in C#

public double Ceiling(double value, double factor)
{
    return Math.Ceiling(value / factor) * factor;
}

I'm posting it here because I needed such a function google sent me to this question but I couldn't find an answer for .Net. I finally figured it out for myself.

Tim Murphy