Solved

Finding 18 and older

Posted on 2009-07-13
5
137 Views
Last Modified: 2012-05-07
I need to Exit the Sub if the Birthdate of a person is 18 and over.  Can someone provide me with code to put on an Exit event?
0
Comment
Question by:al4629740
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:game-master
ID: 24846429

try sumthing like this...



Sub checkYear(bDate As Date)
   
    If (Year(Now) - Year(bDate)) >= 18 Then
        MsgBox "more than 18 years old"
exit sub
    End If

End Sub


game-master
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 24848398
I think that this function will be a bit more accurate
Sub Something()
'...
If IsOver18(TheDate) then
    Exit Sub
end if
end sub
 
Function IsOver18(Birthdate As Date) As Boolean
    Dim BirthdayThisYear As Date
    Dim iYears As Integer
    
    iYears = DateDiff("yyyy", Birthdate, Now)
    BirthdayThisYear = DateAdd("yyyy", iYears, Birthdate)
    If Now < BirthdayThisYear Then
        iYears = iYears - 1
    End If
    If iYears >= 18 Then
        IsOver18 = True
    End If
End Function

Open in new window

0
 
LVL 2

Expert Comment

by:everkleer
ID: 24851872
I could be wrong, but do you really need/want that 'BirthdayThisYear' logic?  I think the DateDiff function would take care of that (It seems that you would only need the 'BirthdayThisYear' logic with something like the solution game-master provided.)
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24854197
everkleer,

The conventional meaning of 'Are you a certain age?' generally means 'How many whole years have passed up until your last birthday?'. Simply year VB calculations do not give that answer. DateDiff just gives you the difference between the respective years and could make one appear a year older than the convention says. At my age, that is an important difference; at least it was, for this and any other year, up until my birthday on the 20th March.
0
 
LVL 2

Expert Comment

by:everkleer
ID: 24854286
Oh I see.  So is the DateDiff function alone without your added logic basically doing the same thing as the other guy suggested (Year(Now) - Year(bDate))?  I assumed the function would perform the extra logic of calculating whether the birthday had occurred yet this year.  Looks like your birthday is 3 days after mine but you are obviously older & wiser than me!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
consolidate or create a new vba code for performing tasks 5 65
change vba from autofit to 13.5 width? 4 29
Validating VB6 Function 19 59
Passing a Text Box name to a Sub 6 96
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now