473,795 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Type

I have a function that calculates a period of time e.g. 2.3 years, .5
years etc. I was using the function in a query, but since I had not
declared a data type for the function, it wasn't sorting numerically
in my query. Can you help me figure out which data type is appropriate
for my function. I tried type Long, but that doesn't seem to include
decimals. I tried Single, but I can't seem to round it to just one
decimal (I tried in the actual function, and I also tried in the
query).

Here is my function:

Function LOSActives(emp As Date) As Single
LOSActives = Round((DateDiff ("d", emp, Date)) / 365.25, 1)

End Function

And here is how I'm using it in my query...

LengthService: losactives([activeemp]![empdate])

And I am using Access 2000. Thanks.

Oct 8 '07 #1
1 2196
In general, use Double for fractional numbers and Long for whole numbers.

Your function accepts an argument of type Date. That's fine as long as you
are certain you will never pass in a Null when calling this function.
Examples where a Null might get passed in are:
- If the field is not Required in your table;
- If the function might be called from the new record row in a form;
- If the query contains outer joins.

If any of those could occur, you need to use:
Function LOSActives(emp As Variant) As Double
If IsDate(emp) Then
LOSActives = Round((DateDiff ("d", emp, Date)) / 365.25, 1)
End If
End Function

If you want the function to return Null when the date passed in is null,
declare it As Variant instead of As Double.
Function LOSActives(emp As Variant) As Variant
If IsDate(emp) Then
LOSActives = Round((DateDiff ("d", emp, Date)) / 365.25, 1)
Else
LOSActives = Null
End If
End Function

But this brings you full circle to the problem where they query treats it as
Text instead of numeric. You then have to trick JET into recognising it as a
number in the query, like this:
LengthService: IIf(False, 0, losactives([activeemp]![empdate]))

False will never be True, so the 0 will never be used, but just having the
alternative there is enough to give JET the intended data type.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Coll" <co*********@ho tmail.comwrote in message
news:11******** *************@y 42g2000hsy.goog legroups.com...
>I have a function that calculates a period of time e.g. 2.3 years, .5
years etc. I was using the function in a query, but since I had not
declared a data type for the function, it wasn't sorting numerically
in my query. Can you help me figure out which data type is appropriate
for my function. I tried type Long, but that doesn't seem to include
decimals. I tried Single, but I can't seem to round it to just one
decimal (I tried in the actual function, and I also tried in the
query).

Here is my function:

Function LOSActives(emp As Date) As Single
LOSActives = Round((DateDiff ("d", emp, Date)) / 365.25, 1)

End Function

And here is how I'm using it in my query...

LengthService: losactives([activeemp]![empdate])

And I am using Access 2000. Thanks.
Oct 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3740
by: Gord | last post by:
Hello, I think that what I'm trying to do is impossible, but before I give up I thought I'd try and pick a few more knowledgeable brains than my own. I have any array of user defined type variables. I need to loop through the array (doing certain calculations) on a particular member variable. I then want to loop through the array again on a different member variable doing very similar calculations. I only need to change a couple...
3
1959
by: Mike Conmackie | last post by:
Greetings, I am trying to create a node in the output tree using a variable. Here are some fragments that I hope will explain the problem better. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:glbl="http://www.compuware.com/XSL/globalvariables" version="1.0"> <glbl:host-id-types> <glbl:host-id-type t="undefined">unspecified</glbl:host-id-type>
1
4561
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable name="Tim">ZXCVBNM</Variable> <Function id="1"> <Parameter type="String">Bob</Parameter> <Parameter type="String">Steve</Parameter>
7
1825
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump follows... I need a class to represent a variable, with an associated data type and/or value. Though, I don't want it to be a variant type, and not a
134
7919
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
14
2546
by: sathya_me | last post by:
Dear clc, I have a variable void *a; Since variable "a" can be assigned (point to) any type and also any type can be assigned to "a" (i.e means "a" = any typed variable; any typed variable = "a". Considering the above I have a function, which is declared and defined to take any type of parameter with void* return-type foo (void *a); In the processes of assignment of value to the variable "a" I want to
8
5028
by: Groups User | last post by:
C allows type casting in which a variable is converted from one type to another. Does C (whatever standard) allow the type of a variable to change, within a statement, avoiding the conversion? And if so, provide an example of how its done. Example: int a=23; int b=34;
20
7013
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
13
2028
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable "myvariable" and then maybe assign it something. myvariable = "this is a real variable"
112
5486
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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,...
0
10002
tracyyun
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...
1
7543
isladogs
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...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.