Hi everyone,
I have a code that I can use in Excel to round to the nearest 0.5, but I cannot get it to work in Access. When I try to execute it I get the following error message: "Compile error: user defined type not defined." Any idea what I need to do to change my code to work in Access?
Here's the code: -
'Round to nearest 0.5
-
Public Function vbaRoundTO(dblValue As Double, dblRoundTo As Double, _
-
Optional RoundingOption As rOpt = rNearest) As Double
-
Dim dblRoundedMutliple As Double
-
Dim dblValueDiv As Double
-
Dim dblValueNew As Double
-
-
'Set default retrun value if dblRoundTo = 0
-
If dblRoundTo = 0 Then
-
vbaRoundTO = 0 'OR vbaRoundTO = dblValue
-
Exit Function
-
End If
-
-
'Find multiple of RoundToSmallest
-
dblValueDiv = dblValue / dblRoundTo
-
-
'Option to RoundUP or RoundDOWN
-
Select Case RoundingOption
-
Case rNearest
-
'Round multiple to nearest
-
'DO NOT USE VBA Round() function.
-
'VBA : Round(2.5,0) = 2, i.e. rounds >=0.5 to 0 not 1
-
dblRoundedMutliple = vbaRound(dblValueDiv, 0)
-
Case rUp
-
'Round multiple UP
-
dblRoundedMutliple = vbaRound(dblValueDiv, 0, 1)
-
Case rDown
-
'Round multiple DOWN
-
dblRoundedMutliple = vbaRound(dblValueDiv, 0, 2)
-
Case Else
-
End Select
-
-
'Calculate new "rounded-to" value
-
dblValueNew = dblRoundedMutliple * dblRoundTo
-
-
'Return value
-
vbaRoundTO = dblValueNew
-
-
End Function
-
Thank you for helping!
Linda
1 5381
I figured out the problem. I had failed to copy over all the necessary code. In case anyone needs it. Here it is.
Code: -
'Set variables
-
Public Enum rOpt
-
rNearest
-
rUp
-
rDown
-
End Enum
-
'Rounding function
-
Public Function vbaRound(dblValue As Double, intDecimals As Integer, _
-
Optional RoundingOption As rOpt = rNearest) As Double
-
Dim dblPlacesFactor As Double
-
Dim dlbRoundFactor As Double
-
-
If intDecimals < 0 Then
-
vbaRound = 0
-
Exit Function
-
End If
-
-
dblPlacesFactor = 10 ^ intDecimals
-
-
Select Case RoundingOption
-
Case rNearest 'Round to Nearest
-
dlbRoundFactor = 0.5
-
Case rUp 'Round UP
-
dlbRoundFactor = 1
-
Case rDown 'Round DOWN
-
dlbRoundFactor = 0
-
End Select
-
-
vbaRound = Int(dblValue * dblPlacesFactor + dlbRoundFactor) / dblPlacesFactor
-
End Function
-
'Round to nearest 0.5
-
Public Function vbaRoundTO(dblValue As Double, dblRoundTo As Double, _
-
Optional RoundingOption As rOpt = rNearest) As Double
-
Dim dblRoundedMutliple As Double
-
Dim dblValueDiv As Double
-
Dim dblValueNew As Double
-
-
'Set default retrun value if dblRoundTo = 0
-
If dblRoundTo = 0 Then
-
vbaRoundTO = 0 'OR vbaRoundTO = dblValue
-
Exit Function
-
End If
-
-
'Find multiple of RoundToSmallest
-
dblValueDiv = dblValue / dblRoundTo
-
-
'Option to RoundUP or RoundDOWN
-
Select Case RoundingOption
-
Case rNearest
-
'Round multiple to nearest
-
'DO NOT USE VBA Round() function.
-
'VBA : Round(2.5,0) = 2, i.e. rounds >=0.5 to 0 not 1
-
dblRoundedMutliple = vbaRound(dblValueDiv, 0)
-
Case rUp
-
'Round multiple UP
-
dblRoundedMutliple = vbaRound(dblValueDiv, 0, 1)
-
Case rDown
-
'Round multiple DOWN
-
dblRoundedMutliple = vbaRound(dblValueDiv, 0, 2)
-
Case Else
-
End Select
-
-
'Calculate new "rounded-to" value
-
dblValueNew = dblRoundedMutliple * dblRoundTo
-
-
'Return value
-
vbaRoundTO = dblValueNew
-
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: b |
last post by:
Hello all, we have a table that is part of our accounting package that
stores decimals as such: 123.45678.
However the reporting standards with the accounting system display that as
123.45 note that it does not round to the 123.46. I need for these reports
to match up however in my datagrid which i have set for currency it does the
rounding.. Does anyone know a way around this..
I Have looked around on the web but yet to find anything...
|
by: calan |
last post by:
Does anyone have a function that will round a number to 0 or .5?
I have a form where I'm entering a number in inches. I need to round it to
the nearest 1/2 inch (onChange).
The split will be on increments of .25
22.24 = 22.0
22.25 = 22.5
22.52 = 22.5
|
by: Zorpiedoman |
last post by:
Howcome:
Dim D as decimal = .5D
msgbox d.Round(D, 0)
this returns "0"
Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!!
Documentation says this, but what the heck are they thinking??? I just don't
|
by: Jeff Boes |
last post by:
(asked last week on .questions, no response)
Can anyone explain why this happens? (under 7.4.1)
select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ;
timestamp
---------------------
2004-05-27 09:00:01
|
by: 6tc1 |
last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure
this is an old issue, but it is new to me and resulted in a fair amount
of time trying to track down the issue.
Basically put the following code into your C# app:
float testFloat2 = (int) (4.2f * (float)100);
Console.Out.WriteLine("1: "+testFloat2);
and the result will be 419
| |
by: John |
last post by:
Hi
I am calculating tax as follows;
= * / 100#
Client complains that there is sometimes difference in pennies in tax
calculation. Is there a better (more precise) way to calculate tax?
Thanks
|
by: Spoon |
last post by:
Hello everyone,
I don't understand how the lrint() function works.
long lrint(double x);
The function returns the nearest long integer to x, consistent with the
current rounding mode. It raises an invalid floating-point exception if
the magnitude of the rounded value is too large to represent. And it
raises an inexact floating-point exception if the return value does not
|
by: md |
last post by:
Hi
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
A function like this:
RoundMyDouble (double &value, short numberOfPrecisions)
It then updates the value with numberOfPrecisions after the decimal
|
by: jacob navia |
last post by:
Hi
"How can I round a number to x decimal places" ?
This question keeps appearing. I would propose the following
solution
#include <float.h>
#include <math.h>
|
by: bdsatish |
last post by:
The built-in function round( ) will always "round up", that is 1.5 is
rounded to 2.0 and 2.5 is rounded to 3.0.
If I want to round to the nearest even, that is
my_round(1.5) = 2 # As expected
my_round(2.5) = 2 # Not 3, which is an odd num
I'm interested in rounding numbers of the form "x.5" depending upon
whether x is odd or even. Any idea about how to implement it ?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |