Results 1 to 2 of 2

Thread: Calling a Sub or function

  1. #1
    Guest
    I have this sub procedure used to calculate the age of a person in years, months and days.
    Public Sub CalcAge(vDate1 As Date, vdate2 As Date, ByRef vYears As Integer,
    ByRef vMonths As Integer, ByRef vDays As Integer)
    ' Comments : calculates the age in Years, Months and Days
    ' Parameters:
    ' vDate1 - D.O.B.
    ' vDate2 - Date to calculate age based on
    ' vYears - will hold the Years difference
    ' vMonths - will hold the Months difference
    ' vDays - will hold the Days difference
    vMonths = DateDiff("m", vDate1, vdate2)
    vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2)
    If vDays < 0 Then
    ' wierd way that DateDiff works, fix it here
    vMonths = vMonths - 1
    vDays = DateDiff("d", DateAdd("m", vMonths, vDate1), vdate2)
    End If
    vYears = vMonths \ 12 ' integer division
    vMonths = vMonths Mod 12 ' only want leftover less than one year
    End Sub
    My Question is how do I call this sub so that the age of each person will appear in a text box on a form with staff or students information each time a user loads the form. I have been trying several times with failer.

  2. #2
    Addicted Member
    Join Date
    Sep 2000
    Posts
    230
    Gots,

    You would want to use a function procedure if returning a value. Below is general function statement and the call:

    Code:
    Private Sub Form_Load()
    
    thenumber = 4
    
    Me.Text1.Text = general(thenumber)
    
    End Sub
    
    
    Function general(anyValue As Long) As Long
    
    general = anyValue * 2
    
    End Function
    All you do is call the function where you want the value (or you can set the function = to a variable) and send it the parameters, in your case, the same ones you have in your sub proc. In the function proc., set the name of the function = to the value you want returned to the calling proc. and thats it. If you need more help, let me know.

    Shawn Hull
    VB6, SP3 (Professional Edition)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width