
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