Making a Field visible or invisble depending on another field's da

Making a Field visible or invisble depending on another field's da

Post by SnVhb » Sat, 04 Mar 2006 03:41:28


hi everyone,

i would like to know how to make a field on a form appear or disapear
depending on what value i input in another field.

ex: I have a field called [DOB] and another called [Delegate name] on a
form.
I want for [Delegate name] to appear only when [DOB] has a date that is
14-years-old or older.

i hope this is clear enough,

thanks in advance
 
 
 

Making a Field visible or invisble depending on another field's da

Post by Bruce » Sat, 04 Mar 2006 03:58:24

One way is to base the form on a query, and add to the query a field
something like this:
Age:
DateDiff("yyyy",[DoB],Date())-IIf(Format([DoB],"mmdd")>Format(Date(),"mmdd"),1,0)

In the form's current event:

If Me.Age < 14 Then
Me.txtDelegateName.Visible = False
Else
Me.txtDelegateName.Visible = True
End If

John Vinson supplied the Age expression several years ago in a post I found
just recently through a newsgroups search.

 
 
 

Making a Field visible or invisble depending on another field's da

Post by S2xhdHV » Sat, 04 Mar 2006 04:15:26

In case you need a function to correctly calculate a person's age, here is one:

Function Age(Bdate, DateToday) As Integer
' Returns the Age in years between 2 dates
' Doesn't handle negative date ranges i.e. Bdate > DateToday

If Month(DateToday) < Month(Bdate) Or (Month(DateToday) = _
Month(Bdate) And Day(DateToday) < Day(Bdate)) Then
Age = year(DateToday) - year(Bdate) - 1
Else
Age = year(DateToday) - year(Bdate)
End If
End Function

Now, to your question. There are a few places you need to address in your
form to make this work correctly. First, set the Visible property of
[delegate name] to No in design view.

I would suggest you put the function above in a standard module so other
places in your app can use it. I have a module named modDateFunctions that I
keep all my date handling tricks in.

In the After Update event of [DOB]

Me.[delegate name].visible = Age(Me.[DOB], Date) >= 14

Put this in the Current event of the form:

If Me.NewRecord then
Me.[delegate name].visible = False
Else
Me.[delegate name].visible = Age(Me.[DOB], Date) >= 14
End If