Calculate age by date of birth

0

I am trying to calculate the age in a field of a form through the date of birth, but it returns me the compilation error 13 type mismatch . Please someone help me? Here is the CODE.

Private Sub CmbCalcular_Click()

    Dim datanasc As Date, idade As Integer

    datanasc = CDate(MskDataNasc.Mask)
    idade = CInt((Date - datanasc) / 365)
    TxtIdade = Str(idade) & "anos"

End Sub
    
asked by anonymous 20.10.2016 / 15:20

1 answer

0

Use IsDate(valor) that will return true if the date is valid, use Text to pick up the content value % , that is, you have three problems in your code:

>

Solution:

Private Sub CmbCalcular_Click()    
    Dim datanasc As Date, idade As Integer
    If IsDate(MskDataNasc.Text) Then
          datanasc = CDate(MskDataNasc.Text)
          idade = CInt((Now - datanasc) / 365)
          TxtIdade = Str(idade) & "anos"
    Else
          TxtIdade.Text = "Data inválida" 
    End If    
End Sub

The error informed type mismatch , means that you tried to convert for a particular type, but this conversion is not valid, then use MaskedBox before to check if it is possible to convert to Date

    
20.10.2016 / 15:24