434,871 Members | 2,368 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Data type interpretation problem

 P: 3 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 Function roottransaction(FormulaItemNumber As Variant, FocusNumber As Variant) As Integer Dim varx As Variant, vary As Variant, x As Integer     varx = FormulaItemNumber x = 0 Do     vary = DLookup("[Previousid]", "FormulaItemsDescriptionView", "[FormulaItemNumber] =" & varx)     If vary = 0 Then         varx = 0         x = 1     ElseIf (vary = FocusNumber) Then         varx = 1         x = 1     Else         varx = vary     End If Loop Until x = 1   roottransaction = varx End Function Aug 11 '08 #1
3 Replies

 Expert Mod 15k+ P: 31,489 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 Public Function RootTransaction(FormulaItemNumber As Double, _                                 FocusNumber As Double) As Integer   Dim dblX As Double, dblY As Double   Dim blnLoop As Boolean     dblX = FormulaItemNumber   blnLoop = True   Do While blnLoop       dblY = DLookup("[Previousid]", _                      "[FormulaItemsDescriptionView]", _                      "[FormulaItemNumber]=" & dblX)       If dblY = 0 Then           dblX = 0           blnLoop = False       ElseIf (dblY = FocusNumber) Then           dblX = 1           blnLoop = False       Else           dblX = dblY       End If   Loop     RootTransaction = dblX 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

 Expert 5K+ P: 8,637 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: Function Definition with Integer Return Value: Expand|Select|Wrap|Line Numbers Public Function fTest(intOne As Integer, intTwo As Integer) As Integer   fTest = (intOne / intTwo) End Function Debug.Print fTest(1, 3) yields 0 Function Definition with Double Return Value: Expand|Select|Wrap|Line Numbers Public Function fTest(intOne As Integer, intTwo As Integer) As Double   fTest = (intOne / intTwo) End Function Debug.Print fTest(1, 3) yields 0.333333333333333 Try: Expand|Select|Wrap|Line Numbers Public Function RootTransaction(FormulaItemNumber As Double, _                                 FocusNumber As Double) As Double ... Any questions feel free to ask. Aug 12 '08 #3 