473,471 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Data type interpretation problem

3 New Member
I'm trying to return a number from the following function. The number is defined as double, and I tried declaring all my variables as double prior to variant. If i substitute focusnumber with a specific example of a number I'm trying to search (5.002 for example) I receive the desired result. Otherwise varx always =0. Please help.
Expand|Select|Wrap|Line Numbers
  1. Function roottransaction(FormulaItemNumber As Variant, FocusNumber As Variant) As Integer
  2. Dim varx As Variant, vary As Variant, x As Integer
  3.  
  4.  
  5. varx = FormulaItemNumber
  6. x = 0
  7. Do
  8.     vary = DLookup("[Previousid]", "FormulaItemsDescriptionView", "[FormulaItemNumber] =" & varx)
  9.     If vary = 0 Then
  10.         varx = 0
  11.         x = 1
  12.     ElseIf (vary = FocusNumber) Then
  13.         varx = 1
  14.         x = 1
  15.     Else
  16.         varx = vary
  17.     End If
  18. Loop Until x = 1
  19.  
  20. roottransaction = varx
  21. End Function
Aug 11 '08 #1
3 1277
NeoPa
32,556 Recognized Expert Moderator MVP
I'm afraid the explanation seems as much of a mess as the code.

This code appears to be half way through an experimental stage. That's not very well appreciated. It puts the extra work of trying to work out what's going on down to the experts and that's not, nor should ever be, our responsibility.

You have various variables defined. Some are variant and some integer, yet you claim that the function should be returning a double.
Expand|Select|Wrap|Line Numbers
  1. Public Function RootTransaction(FormulaItemNumber As Double, _
  2.                                 FocusNumber As Double) As Integer
  3.   Dim dblX As Double, dblY As Double
  4.   Dim blnLoop As Boolean
  5.  
  6.   dblX = FormulaItemNumber
  7.   blnLoop = True
  8.   Do While blnLoop
  9.       dblY = DLookup("[Previousid]", _
  10.                      "[FormulaItemsDescriptionView]", _
  11.                      "[FormulaItemNumber]=" & dblX)
  12.       If dblY = 0 Then
  13.           dblX = 0
  14.           blnLoop = False
  15.       ElseIf (dblY = FocusNumber) Then
  16.           dblX = 1
  17.           blnLoop = False
  18.       Else
  19.           dblX = dblY
  20.       End If
  21.   Loop
  22.  
  23.   RootTransaction = dblX
  24. End Function
Try this out and see what happens.

If it fails then we need a clear and precise explanation of what goes wrong. Quote line numbers where necessary.

In future, please put your question together with a little more care before posting.
Aug 11 '08 #2
ADezii
8,834 Recognized Expert Expert
The Return Value of your Function is set to Integer meaning you will 'never', 'ever' retrieve a Double from this Function. The Function Return Value, assuming it would be a Double, would be coerced to an Integer. A simple case will illustrate my point:
  1. Function Definition with Integer Return Value:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fTest(intOne As Integer, intTwo As Integer) As Integer
    2.   fTest = (intOne / intTwo)
    3. End Function
    Debug.Print fTest(1, 3) yields 0
  2. Function Definition with Double Return Value:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fTest(intOne As Integer, intTwo As Integer) As Double
    2.   fTest = (intOne / intTwo)
    3. End Function
    Debug.Print fTest(1, 3) yields 0.333333333333333
  3. Try:
    Expand|Select|Wrap|Line Numbers
    1. Public Function RootTransaction(FormulaItemNumber As Double, _
    2.                                 FocusNumber As Double) As Double
    3. ...
  4. Any questions feel free to ask.
Aug 12 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. To add to NeoPa and Adezii's comments, the choice of name for your function does not help with self-documenting it (what does 'roottransaction' mean to anyone reading it)? And there are no comments in your code to explain what it is trying to do.

In line with NeoPa's comments I did try to read your code but could not work out what it was doing.

On the use of numbers Adezii has pointed out that you are returning an integer value from your function, so it can never return a double. I would also point out two things: (1) using variants for numeric variables is a mistake, unless you really want and need Access to determine what the precision of a value should be (guessing on your behalf every time), and (2) you are using comparisons for equality on values which are (or are supposed to be) floating point (i.e. single or double variables). Floating point values are always subject to small representational errors (of the order of 10^-11 to 10^-14) which will cause comparisons for equality to fail, even when the values are the same to 10 decimal places. In these circumstances you need to define a 'near-equality' threshold to which you compare the absolute value of the difference if you wish to get floating point comparisons to work reliably. The threshold chosen depends on your application's requirements and the precision of the data (as well as the variables representing that data).

Example:

Expand|Select|Wrap|Line Numbers
  1. Const NearZeroValue = 0.000000001
  2. If abs (val1 - val2) < NearZeroValue then ...
-Stewart
Aug 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nicolas Keller | last post by:
Hi! I'm used to have Mozilla for testing my PHP sites when I'm coding. The site's nearly finished, now I've made a test with the Internet Exlporer... guess what... failed. The problem: I'm...
21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
3
by: John Dunlop | last post by:
(Note crosspost and follow-ups to ciwah.) Nicolas Keller wrote in thread "Differences in form handling btw Mozilla and IE?": > The problem: I'm using a form that submit's (POST) its data via...
0
by: JR | last post by:
Hello, Although I consider myself a proficient C++ programmer (and am getting better everyday at C# :-) I am new to and having a little trouble with some of the concepts behind ASP and ASP.NET....
3
by: adgnews | last post by:
I have a problem converting a program from Vb.net VS2003 to VS2005. I process a xml-file with an inline schema. In 2003 no problem. In 2005 I have 2 problems: 1) BIG space="preserve" problem....
1
by: jrs_14618 | last post by:
Hello All, This post is essentially a reply a previous post/thread here on this mailing.database.myodbc group titled: MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode I was...
5
by: Dave | last post by:
Hello all Is there a yes/no boolean datatype with mySQL? I can't seem to find if there is, and I have used an int type set to 1 or 0 but that breaks some of my apps that used to use access which...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
10
by: Will Honea | last post by:
I have a data set which I need to analyze but I am having a problem figuring out a structure for the database - or whether there are better ways of attacking the problem. The base data set is a...
2
by: manugm1987 | last post by:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <style type="text/css"><!--...
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
Oralloy
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,...
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...
1
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
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.