473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2178
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*********@hotmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.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
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...
3
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...
1
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...
7
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...
134
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...
14
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 =...
8
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?...
20
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 =...
13
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...
112
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
agi2029
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,...

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.