Solved

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

Posted on 2014-03-20
18
2,787 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

920 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

15 Experts available now in Live!

Get 1:1 Help Now