473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ms Access Round Up

I need to create a Roundup function in Access for example.....

36.11 to 36.2
44.43 to 44.5
55.55 to 55.6
32.51 to 32.6
21.67 to 21.7

PS: I'm an amateur at writing VBA code and creating modules within Access
Oct 8 '07 #1
13 12122
Scott Price
1,384 Expert 1GB
Hi Te,

Welcome to the Scripts!

You have inadvertently posted your question in our Articles section. I have moved it across for you to our main forum where it is much more likely to get a response.

MODERATOR
Oct 8 '07 #2
Scott Price
1,384 Expert 1GB
The normal function that VBA/Access provides is called Round(), however it will not do what you are asking, since it rounds to the nearest decimal place, not the next highest decimal place.

This being the case, you'll have to write a Select Case function to do what you are asking.

This should do it:

Expand|Select|Wrap|Line Numbers
  1. Public Function RoundUp(ToRound As Double) As String
  2.  
  3.   Dim FixedQty As Long
  4.   Dim Temp As String
  5.   Dim TotQty As Double
  6.  
  7. TotQty = ToRound
  8. FixedQty = Fix(TotQty)
  9.  
  10.   If FixedQty > 0 Then
  11.     Temp = CStr(FixedQty)
  12.   Else
  13.     Temp = CStr(0)
  14.   End If
  15.  
  16.   Select Case TotQty - FixedQty
  17.     Case 0.01 To 0.09
  18.       Temp = Temp + 0.1
  19.     Case 0.11 To 0.19
  20.       Temp = Temp + 0.2
  21.     Case 0.21 To 0.29
  22.       Temp = Temp + 0.3
  23.     Case 0.31 To 0.39
  24.       Temp = Temp + .4
  25.     Case 0.41 To 0.49
  26.       Temp = Temp + 0.5
  27.     Case 0.51 To 0.59
  28.       Temp = Temp + 0.6
  29.     Case 0.61 To 0.69
  30.       Temp = Temp + 0.7
  31.     Case 0.71 To 0.79
  32.       Temp = Temp + 0.8
  33.     Case 0.81 To 0.89
  34.       Temp = Temp + 0.9
  35.     Case Is > 0.91
  36.       Temp = CStr(Int(TotQty) + 1)
  37.   End Select
  38.  
  39.   RoundUp = Temp
  40.  
  41. End Function
This code will go into a standard code module (In VBA editor window, click Insert>Module). You can then call it from a query, a control on a form, etc. Just pass it the number to round and it will return the result.

Caveats:

This assumes that you will be working with two decimal places and no more... If you have a number of 1.001 this function won't fire to round it to 1.1 (nor should there be any need to that I can see...)

This will return the number formatted as a string. If you need it formatted as a number we'll have to change the declarations.

Regards,
Scott
Oct 8 '07 #3
FishVal
2,653 Expert 2GB
I need to create a Roundup function in Access for example.....

36.11 to 36.2
44.43 to 44.5
55.55 to 55.6
32.51 to 32.6
21.67 to 21.7

PS: I'm an amateur at writing VBA code and creating modules within Access
Hi, Kuini.

Expand|Select|Wrap|Line Numbers
  1. Public Function RoundUp(varInput As Variant, intDigits As Integer) As Variant
  2.     RoundUp = -Int(-varInput * 10 ^ intDigits) / 10 ^ intDigits
  3. End Function
  4.  
Take a look also at this .
Oct 8 '07 #4
Great, thanks Scott. I''ve inserted the code into a new module however, I've tried to call the function from a query (Kgs/Sheet: RoundUp([Th Kgs]) but I receive the error message "Undefined function 'Roundup' in expression".

Also, I need the result to be formatted as a number not as a string.

Many thanks, Te Kuini

The normal function that VBA/Access provides is called Round(), however it will not do what you are asking, since it rounds to the nearest decimal place, not the next highest decimal place.

This being the case, you'll have to write a Select Case function to do what you are asking.

This should do it:

Expand|Select|Wrap|Line Numbers
  1. Public Function RoundUp(ToRound As Double) As String
  2.  
  3.   Dim FixedQty As Long
  4.   Dim Temp As String
  5.   Dim TotQty As Double
  6.  
  7. TotQty = ToRound
  8. FixedQty = Fix(TotQty)
  9.  
  10.   If FixedQty > 0 Then
  11.     Temp = CStr(FixedQty)
  12.   Else
  13.     Temp = CStr(0)
  14.   End If
  15.  
  16.   Select Case TotQty - FixedQty
  17.     Case 0.01 To 0.09
  18.       Temp = Temp + 0.1
  19.     Case 0.11 To 0.19
  20.       Temp = Temp + 0.2
  21.     Case 0.21 To 0.29
  22.       Temp = Temp + 0.3
  23.     Case 0.31 To 0.39
  24.       Temp = Temp + .4
  25.     Case 0.41 To 0.49
  26.       Temp = Temp + 0.5
  27.     Case 0.51 To 0.59
  28.       Temp = Temp + 0.6
  29.     Case 0.61 To 0.69
  30.       Temp = Temp + 0.7
  31.     Case 0.71 To 0.79
  32.       Temp = Temp + 0.8
  33.     Case 0.81 To 0.89
  34.       Temp = Temp + 0.9
  35.     Case Is > 0.91
  36.       Temp = CStr(Int(TotQty) + 1)
  37.   End Select
  38.  
  39.   RoundUp = Temp
  40.  
  41. End Function
This code will go into a standard code module (In VBA editor window, click Insert>Module). You can then call it from a query, a control on a form, etc. Just pass it the number to round and it will return the result.

Caveats:

This assumes that you will be working with two decimal places and no more... If you have a number of 1.001 this function won't fire to round it to 1.1 (nor should there be any need to that I can see...)

This will return the number formatted as a string. If you need it formatted as a number we'll have to change the declarations.

Regards,
Scott
Oct 8 '07 #5
Thanks FishVal. Will give it a go.

Hi, Kuini.

Expand|Select|Wrap|Line Numbers
  1. Public Function RoundUp(varInput As Variant, intDigits As Integer) As Variant
  2.     RoundUp = -Int(-varInput * 10 ^ intDigits) / 10 ^ intDigits
  3. End Function
  4.  
Take a look also at this .
Oct 8 '07 #6
missinglinq
3,532 Expert 2GB
I''ve inserted the code into a new module
You didn't name the module the same name as the function, did you? This is a common mistake,and it confuses the Access gnomes! If the function and the module have the same name, change the module's name.

Welcome to TheScripts!

Linq ;0)>
Oct 9 '07 #7
Ooops thanks Linq...I've changed the name of the module and it works just fine


You didn't name the module the same name as the function, did you? This is a common mistake,and it confuses the Access gnomes! If the function and the module have the same name, change the module's name.

Welcome to TheScripts!

Linq ;0)>
Oct 9 '07 #8
Hi Scott - sometimes I get the wrong result from the round up function e.g.:

36.10 returns 36
72.90 " 72
36.11 " 36
32.51 " 32

Can you help????

Great, thanks Scott. I''ve inserted the code into a new module however, I've tried to call the function from a query (Kgs/Sheet: RoundUp([Th Kgs]) but I receive the error message "Undefined function 'Roundup' in expression".

Also, I need the result to be formatted as a number not as a string.

Many thanks, Te Kuini
Oct 9 '07 #9
I have to say, after spending an entire afternoon of looking everywhere to get some help with what seemed like a simple problem, you finally helped me.

Thank you, thank you, thank you!

Leslie
Oct 11 '07 #10
Scott Price
1,384 Expert 1GB
Hi Scott - sometimes I get the wrong result from the round up function e.g.:

36.10 returns 36
72.90 " 72
36.11 " 36
32.51 " 32

Can you help????
Hi Te Kuini,

This likely is resulting because of the String data type. Try changing from String to Double (in the first line of the function code).

Change the declaration for the Temp variable from String to Double as well.

Regards,
Scott
Oct 12 '07 #11
NeoPa
32,556 Expert Mod 16PB
Thanks FishVal. Will give it a go.
I must register in this thread.
I saw it the other day then, coincidentally, needed to reproduce it for my own purposes. I hunted everywhere for it (assuming it was a registered thread of course) but couldn't find it. Luckily enough I had remembered the gist of what FishVal had posted (Post #4) and, as it happens, it seems to match exactly what I remembered. Of course it works (very elegant code I would say) and now I have a version of it in my database module :)

BTW I'm curious how you got on with it?
No disrespect to you, Scott, or your code, but the one-liner seems to do the whole job without complication.
Oct 12 '07 #12
Scott Price
1,384 Expert 1GB
Actually I'm impressed with Fish's code also :-)

If the OP wishes to go with that solution (which is more elegant, yes) than I'd be quite happy with that also!

That said, in defense of the Select Case method: It's more flexible for expanding to different uses, for example, assigning fractional signs to the decimal values (which is actually what I adapted this code from to begin with). However, if the need is confined solely to the rounding, then Fish's code is a simpler solution.

Regards,
Scott
Oct 12 '07 #13
NeoPa
32,556 Expert Mod 16PB
I agree Scott. I'm a big fan of the Select Case construct myself, and the scenario you describe would be a good use for it.
Oct 12 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
6
by: Michael Bulatovich | last post by:
I have a very simple db I use for keeping track of hours, tasks, projects, clients etc. It has a form that I use to enter data. Currently the form has a textbox for a field called "start time",...
5
by: Jozef | last post by:
Does anyone know if there is some sort of catch all installation program that will determine if dependencies were installed properly? I get the feeling that the Windows boys like to throw curve...
2
by: salah961 | last post by:
Hi For example, if I have one of the numbers 6.4, 6.3, 6.2, 6.1 I want the number to be 6 and if the number is 6.6, 6.7, 6.8, 6.9 I want the number to be 7 but I want the number 6.5 without any...
70
by: lgbjr | last post by:
Hello All, I've been developing a VB.NET app that requires the use of a DB. Up to now, I've been using Access. It's a bit slow, but everything works. I'm at a point now where I need to decide if...
62
by: Ecohouse | last post by:
I was just wondering if there was any way to use a toolbar in Outlook 2002 in Access 2002? I want to create a custom toolbar in Access similar to the Calendar toolbar in Outlook. Any ideas?
38
by: shussai2 | last post by:
Hi, I am trying to access Serial Port in XP. I am using Dev-C++ IDE that uses Mingw as a compiler. I just want to know how I can open up serial port on COM1 and write some data. I have searched...
1
by: Corey | last post by:
Hello everyone Can anyone help me convert the sql statement below to a statement I can put into Microsoft access, or help me brake this down so I can put it into access. Thanks Corey ...
2
by: deanoooo812 | last post by:
I have an Access query (written in MS Access 2000 - thats all we've got - don't get me started on that topic...) for making pharmacy dispensing labels based on an extract from an automated dispensing...
1
MMcCarthy
by: MMcCarthy | last post by:
Access has a number of built-in functions which can be generally used in queries or VBA code. Some of the more common ones are: Note: anything in square brackets is optional Date Functions ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.