Passing Value from Module Function to Form sub problem 
Author Message
 Passing Value from Module Function to Form sub problem

In the below code what I'm wanting to do is retrieve a calculated
value from the Function MonthsLeft in Module clsSalaryLeft and return
it to an unbound textbox in the Form frmEEMonths.
I've stepped through the process and the function does all the correct
calcs, and even passes the correct value to intx in the form sub,
however the value won't pass to the textbox.

Dim intx as long

sub Form_Load()
    Dim clsSalLeft As New clsSalaryLeft

    txtEFiscalSal = clsSalLeft.MonthsLeft(intx) 'the value from
MonthsLeft
                                                'is passed to intx
                                                'but txtEFiscalSal is
0

end sub

Function MonthsLeft(cursalary As Long) As Long
    'Calc how many months 'til the end of fiscal
    Set MyDB = CurrentDb
    Set MyTable = MyDB.OpenRecordset("tblEEmonths")

    MyTable.Edit
    datEFiscal = #3/31/2003#

    strMonth = DateDiff("m", Now, datEFiscal) + 1

    intMonth = strMonth

    cursalary = 0
    For intcount = intMonth To 12
        cursalary = cursalary + MyTable("txtsal" & intcount)
    Next intcount
    Mytable.Close
End Function

Help is greatly appreciated

Jason



Tue, 03 May 2005 05:27:22 GMT  
 Passing Value from Module Function to Form sub problem
This doesn't make a whole lot of sense.  Your function
clsSalaryLeft.MonthsLeft is modifying the value of intx (the argument
passed to it), but is returning 0.  Your code in Form_Load() is not
assigning the (modified) value of intx to txtEFiscalSal, but rather
the return-value of the function, which is always 0.

It's usually best if function don't modify the value of the arguments
passed to them, but rather simply return the caluclated value.  Hence
I'd expect you MonthsLeft to be written more like this (air code):

'----- start of revised clsSalaryLeft.MonthsLeft() -----
Function MonthsLeft() As Long

    'Calc how many months 'til the end of fiscal

    Set MyDB = CurrentDb
    Set MyTable = MyDB.OpenRecordset("tblEEmonths")

    datEFiscal = #3/31/2003#

    strMonth = DateDiff("m", Now, datEFiscal) + 1

    intMonth = strMonth

    MonthsLeft = 0
    For intcount = intMonth To 12
        MonthsLeft = MonthsLeft + MyTable("txtsal" & intcount)
    Next intcount

    Mytable.Close

End Function
'----- end of revised clsSalaryLeft.MonthsLeft() -----

*** Note: there are lots of other oddities about this code, and the
whole value can probably be calculated by a simple aggregate query,
but I'm only addressing the return value of the function at the
moment. ***

You'd call the function then like this:

    txtEFiscalSal = clsSalLeft.MonthsLeft()

You'd have no need to pass a variable like intx to the function.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


Quote:
> In the below code what I'm wanting to do is retrieve a calculated
> value from the Function MonthsLeft in Module clsSalaryLeft and
return
> it to an unbound textbox in the Form frmEEMonths.
> I've stepped through the process and the function does all the
correct
> calcs, and even passes the correct value to intx in the form sub,
> however the value won't pass to the textbox.

> Dim intx as long

> sub Form_Load()
>     Dim clsSalLeft As New clsSalaryLeft

>     txtEFiscalSal = clsSalLeft.MonthsLeft(intx) 'the value from
> MonthsLeft
>                                                 'is passed to intx
>                                                 'but txtEFiscalSal
is
> 0

> end sub

> Function MonthsLeft(cursalary As Long) As Long
>     'Calc how many months 'til the end of fiscal
>     Set MyDB = CurrentDb
>     Set MyTable = MyDB.OpenRecordset("tblEEmonths")

>     MyTable.Edit
>     datEFiscal = #3/31/2003#

>     strMonth = DateDiff("m", Now, datEFiscal) + 1

>     intMonth = strMonth

>     cursalary = 0
>     For intcount = intMonth To 12
>         cursalary = cursalary + MyTable("txtsal" & intcount)
>     Next intcount
>     Mytable.Close
> End Function

> Help is greatly appreciated

> Jason



Tue, 03 May 2005 10:18:43 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Passing input box value from Form Module to report module

2. Passing a form to a sub in a module

3. Passing values to a sub or function using ONCLICK

4. Form, module, class and sub or function names at runtime

5. Pass variable value back to form from module

6. Help passing array to bas module sub

7. Incongruous out of memory error when trying to add a function or sub to a module

8. Deleting a Sub or Function from a Module via Code

9. Calling a sub or function using a variable through another sub or function

10. Returning sub or function name within a specific sub or function

11. Local function/sub inside function/sub

12. Retrieve name of current sub/function and name of current module

 

 
Powered by phpBB® Forum Software