Using modules in my Private Sub Function

ariel81

Registered User.
Local time
Today, 07:32
Joined
Dec 31, 2006
Messages
75
Hi,

I have a Public Sub in my modules:
Code:
 Public Sub CalcAge(vDate1 As Date, vdate2 As Date, ByRef vYears As Integer, ByRef vMonths As Integer, ByRef vDays As Integer)

In my form upon cmd button "OK" is clicked, it will use the Public Sub CalcAge function:
Code:
Private Sub cmdOK_Click()

    Dim bday As Date
    Dim input_date As Date
    Dim yrs As Integer
    Dim mths As Integer
    Dim days As Integer


         bday = Me.Text16
         input_date = Me.Text18

         Me.Text20 = CalcAge(bday, input_date, yrs, mths, days)
   
End Sub

- dates in Text16 and Text18 will be input into CalcAge and Text20 will display the "yrs, mths, days" from CalcAge

- however i will get an error msg at the statement :
Code:
Me.Text20 = CalcAge(bday, input_date, yrs, mths, days)

- did i wrote correctly for the abv statement?
 
I see 3 things wrong off the top. In order to return a value, you have to have a function rather than a sub. You haven't specified what it will return. You've declared yrs, mths and days but haven't given them any values, so you're sending Nulls to the function.
 
how shall i correct it?
 
just guessing as you dont show the function, but
Code:
Public Function CalcAge(vDate1 As Date, vdate2 As Date, ByRef vYears As Integer, ByRef vMonths As Integer, ByRef vDays As Integer)

Code:
Private Sub cmdOK_Click()
    Dim bd
    Dim bday As Date
    Dim input_date As Date
    Dim yrs As Integer
    Dim mths As Integer
    Dim days As Integer


         bday = Me.Text16
         input_date = Me.Text18

         bd = CalcAge(bday, input_date, yrs, mths, days)
         Me.Text20 = yrs & " Years, " & mths & " Months and " & days & " Days."
   
End Sub

you should really add some error trapping though to make sure that the dates have been filled in before calling the function. Also try to name your fields usefully.

HTH

Peter
 

Users who are viewing this thread

Back
Top Bottom