Compute age in Visual Basic 6 using DTPicker control

Compute Age
Compute Age

How to compute age in Visual Basic 6 using DTPicker control

This tutorial in Visual Basic 6 will teach us on how to compute our age using DTPicker control.

This is just a simple application that will compute the age based on the date value entered by the user. The application will just need a DTPicker and a TextBox and after the user enters his/her birth date the age will automatically display on the textbox.

Kindly follow the step by step procedure on how build the application.

1. Open your visual basic 6 program and create a new project and select Standard EXE.

2. Add a Form to our project and rename it to ComputeAgeFrm.

3. As stated above, we need a TextBox and DTPicker. Before we can use the DTPicker control, we must add first the Microsoft Windows Common Controls-2 6.0 (SP6) in the components of our visual basic project. Go to Project in the menu bar of Visual Basic and select Components, a dialog box will appear. Kindly scroll down and look for the Microsoft Windows Common Controls-2 6.0 (SP6) check the component and click OK.

4. The controls added to our project are the following:

Animation, UpDown, MonthView, DTPicker and FlatScrollBar

We can find the controls in the toolbox of visual basic.

5. Now that we have added DTPicker on our project, we can now start the application. Kindly add a DTPicker control and name it as dtAgePicker, add also a textbox and name it as txtAge.

6. Kindly add the code below in click event of our DTPicker (dtAgePicker) control.

If IsDate(dtAgePicker.Value) Then
    If Month(Now) - Month(dtAgePicker.Value) = 0 Then
        txtAge.Text = Year(Now) - Year(dtAgePicker.Value)
    Else
        txtAge.Text = Year(Now) - Year(dtAgePicker.Value) - 1
    End If
  Else
    txtAge.Text = ""
End If

7. To make sure you have added the code correctly in the click event of the control. This will be the outcome:

Private Sub dtAgePicker_Click()
If IsDate(dtAgePicker.Value) Then
    If Month(Now) - Month(dtAgePicker.Value) = 0 Then
        txtAge.Text = Year(Now) - Year(dtAgePicker.Value)
    Else
        txtAge.Text = Year(Now) - Year(dtAgePicker.Value) - 1
    End If
  Else
    txtAge.Text = ""
End If
End Sub

8. We also need to add the code in the change event of the DTPicker (dtAgePicker) control, but instead of adding the whole code we need only to call the dtAgePicker_Click event. It should appear as:

Private Sub dtAgePicker_Change()
dtAgePicker_Click
End Sub

9. We’re finally done and ready to test the project.

Hope this simple tutorial will help you.

Your comments and suggestions will really help us to improve our services.

Thank you for visiting inettutor.com.

Comments

comments

, , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.