Solved

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

Posted on 2014-03-20
18
2,732 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now