Avatar billede Claus Mester
03. juli 2002 - 12:50 Der er 2 kommentarer og
2 løsninger

VBAWord: Alder

Hej derude.

Jeg skal have oprettet en kode, der ved indtastning af en persons fødselsdato i en tekstboks, viser personens alder i en label. How?

nicolaus
Avatar billede puppetmaster Nybegynder
03. juli 2002 - 12:53 #1
How do I calculate a person's age?
You can use the DateAdd and DateDiff functions to calculate the time that has elapsed between two dates. With a little additional work, you can present that time in the desired format. For example, the following procedure calculates a person's age in years, taking into account whether his or her birthday has already occurred in the current year.

Using the DateDiff function to determine the number of years between today and a birthdate doesn't always give a valid result, because the DateDiff function rounds to the next year. If a person's birthday hasn't yet occurred, using the DateDiff function will make the person one year older than he or she actually is.

To remedy this situation, the procedure checks to see whether the birthday has already occurred this year; if it hasn't, the procedure subtracts 1 to return the correct age.

Function CalcAge(dteBirthdate As Date) As Long

  Dim lngAge As Long
 
  ' Make sure passed-in value is a date.
  If Not IsDate(dteBirthdate) Then
      dteBirthdate = Date
  End If
 
  ' Make sure birthdate is not in the future.
  ' If it is, use today's date.
  If dteBirthdate > Date Then
      dteBirthdate = Date
  End If
 
  ' Calculate the difference in years between today and birthdate.
  lngAge = DateDiff("yyyy", dteBirthdate, Date)
  ' If birthdate has not occurred this year, subtract 1 from age.
  If DateSerial(Year(Date), Month(dteBirthdate), Day(dteBirthdate)) > Date Then
      lngAge = lngAge - 1
  End If
  CalcAge = lngAge
End Function
Avatar billede kedde65 Praktikant
07. juli 2002 - 16:47 #2
Kan denne kode ikke bruges??

Private Sub Command1_Click()
    Dim birthday As String
    birthday = "06-04-79"
    MsgBox DateDiff("yyyy", birthday, Date)
End Sub


VH CK
Avatar billede Claus Mester
08. juli 2002 - 16:18 #3
Svaret fra puppetmaster, har jeg svært ved at konvertere til brug med en form, en tekstboks og en label. Så om muligt, ville jeg meget gerne have det omformuleret.

Svaret fra kedde65 fungere ikke helt. Når jeg indtaster min søns fødselsdag 14-08-97 får jeg svaret 5 år. Det passer jo ikke helt, idet han lige nu er 4 år - fylder først 5 til august. Hvordan kan den lille forskel omgås?

nicolaus
Avatar billede kedde65 Praktikant
08. juli 2002 - 20:00 #4
Den lille fejl jeg fik lagt ind i mit :-)) Kan rettes ved at gøre det som pupertmaster.
Send mig din word fil, så skal jeg klarede det for dig. Du kender jo mail adressen :-))

VH CK
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester