Solved

Microsoft Access - VBA , Round UP to next EVEN whole number

Posted on 2014-03-20
18
2,854 Views
Last Modified: 2014-03-21
Hi folks,
Trying to round some measurements up to the next "EVEN - WHOLE NUMBER"

2.1 becomes 4
3.7 becomes 4
6.3 becomes 8
7.9 becomes 8

Seems to be a piece of cake in EXCEL with the EVEN function...  can anyone help me with the VBA code to use on a Access 2010 form?
0
Comment
Question by:TechGuise
  • 7
  • 6
  • 5
18 Comments
 
LVL 75
ID: 39944096
Round Up

=-Int(-[YourFieldName])

Round Down:
=Int([YourFieldName])

mx
0
 
LVL 75
ID: 39944099
Be sure to note the  Minus signs :-)
0
 

Author Comment

by:TechGuise
ID: 39944107
Thanks for lightning fast response!  Just tried it.

Me.NewWidth.Value = -Int(-Me.POItemWidth.Value)

22.125 turns into 23
21 just stays at 21

Does what I have look right?
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 75
ID: 39944120
yep.
Guaranteed to work in all case.
Most other schemes fail at some point.

mx
0
 

Author Comment

by:TechGuise
ID: 39944125
Are you sure this will take it to the "NEXT EVEN NUMBER"  (like 2,4,6,8)
0
 

Author Comment

by:TechGuise
ID: 39944128
Has something to do with the way the GLASS INDUSTRY charges for cutting glass
0
 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 250 total points
ID: 39944158
try:
  Dim tmpTXT2 As Double
  tmpTXT2 = Text0
  Text2 = -Int(-Text0)
  If Text2 Mod 2 = 0 Then
    If Text2 - tmpTXT2 < 1 Then
      Exit Sub
    Else
      Text2 = Text2 + 2: Exit Sub
    End If
  End If
  If Text2 = 0 Then Text2 = 2: Exit Sub
  If Text2 = 1 Then Text2 = 2: Exit Sub
  If Text2 \ 2 <> 0 Then Text2 = Text2 + 1
0
 
LVL 75
ID: 39944164
Sorry ... I missed 'EVEN' ...
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 250 total points
ID: 39944177
This should work"

-Int(-[YourFieldName]) + (-Int(-[YourFieldName]) Mod 2)

mx
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39944179
looks much more elegant :-)
0
 

Author Comment

by:TechGuise
ID: 39944182
You guys are awesome!   Both worked great.  Thanks!!
0
 

Author Comment

by:TechGuise
ID: 39944187
Can I ask what MOD 2 does?
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39944199
Divides two numbers and returns only the remainder
and '\' returns the whole number :-) (sometimes called DIV
0
 

Author Comment

by:TechGuise
ID: 39944208
Thank You!
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39944218
you're welcome!
0
 
LVL 75
ID: 39944574
Thanks to Gustav Brock for the elegant Round Up/Down functions .... :-)

mx
0
 
LVL 7

Expert Comment

by:COACHMAN99
ID: 39945581
hear! hear!   (mine was very 'in-elegant') :-)
0
 
LVL 75
ID: 39945784
COACHMAN99 ... you approach was fine. But thanks really to Gustav ... The Master of Date and Time, a more elegant approach exists :-)

mx
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

770 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