disable field

G

Gerald

Dears,

how to disable field with conditons if false

e.g. I have a age field which populates age of a person with weeks. i.e. one
filed the user enters date and on these dates the age populates with weeks
e.g. 1 year 50 weeks

my question

1. if the age is 8 years or above the the date field should disable
2. if the age is 8 weeks or less the text field or date field should disable

hope its possible
plz help

thanks
 
A

Arvin Meyer [MVP]

Code and expressions will work regardless of whether or not the control on
the form is disabled. Disabling only allows you to deny physical data entry.
What you can do is use code instead of an expression to populate the field,
something like (aircode):

Sub txtBirthDate_AfterUpdate
If DateDiff("WW", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else If DateDiff("yyyy", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else
'your date expression here
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
G

Gerald

Thx Arvin, I get a error message "Compile error, else without if"
for below code
Private Sub txtBirthDate_AfterUpdate()

If DateDiff("WW", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

Else:
txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End Sub
 
A

Arvin Meyer

You added a colon after the first Else. Instead of

Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

it should be

Else If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access

Gerald said:
Thx Arvin, I get a error message "Compile error, else without if"
for below code
Private Sub txtBirthDate_AfterUpdate()

If DateDiff("WW", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

Else:
txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End Sub



Arvin Meyer said:
Code and expressions will work regardless of whether or not the control on
the form is disabled. Disabling only allows you to deny physical data entry.
What you can do is use code instead of an expression to populate the field,
something like (aircode):

Sub txtBirthDate_AfterUpdate
If DateDiff("WW", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else If DateDiff("yyyy", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else
'your date expression here
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access

i.e.
one
 
G

Gerald

thanks, but for the below code does not populate the result

Private Sub txtBirthDate_AfterUpdate()

If DateDiff("WW", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

Else
txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End If
End Sub


can u guide ?

thanks

Arvin Meyer said:
You added a colon after the first Else. Instead of

Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

it should be

Else If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access

Gerald said:
Thx Arvin, I get a error message "Compile error, else without if"
for below code
Private Sub txtBirthDate_AfterUpdate()

If DateDiff("WW", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

Else:
txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End Sub



Arvin Meyer said:
Code and expressions will work regardless of whether or not the control on
the form is disabled. Disabling only allows you to deny physical data entry.
What you can do is use code instead of an expression to populate the field,
something like (aircode):

Sub txtBirthDate_AfterUpdate
If DateDiff("WW", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else If DateDiff("yyyy", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else
'your date expression here
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access

Dears,

how to disable field with conditons if false

e.g. I have a age field which populates age of a person with weeks. i.e.
one
filed the user enters date and on these dates the age populates with weeks
e.g. 1 year 50 weeks

my question

1. if the age is 8 years or above the the date field should disable
2. if the age is 8 weeks or less the text field or date field should
disable

hope its possible
plz help

thanks
 
D

Douglas J. Steele

First of all, there's no reason to check for both < 8 weeks and < 8 years.
If they're less than 8 weeks, they're guaranteed to be less than 8 years!
Second, when using the If statement, you can't mix putting multiple
statements on a single line with having the complete structure: since you've
got the Else side, what happens with the If side must be on a separate line:

Private Sub txtBirthDate_AfterUpdate()

If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
Else
Me.txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End If

End Sub


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Gerald said:
thanks, but for the below code does not populate the result

Private Sub txtBirthDate_AfterUpdate()

If DateDiff("WW", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

Else
txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End If
End Sub


can u guide ?

thanks

Arvin Meyer said:
You added a colon after the first Else. Instead of

Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge =
Null

it should be

Else If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge =
Null
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access Downloads
http://www.datastrat.com
http://www.mvps.org/access

Gerald said:
Thx Arvin, I get a error message "Compile error, else without if"
for below code
Private Sub txtBirthDate_AfterUpdate()

If DateDiff("WW", Me.txtBirthDate, Date) <= 8 Then
Me.txtAge = Null
Else: If DateDiff("yyyy", Me.txtBirthDate, Date) <= 8 Then Me.txtAge = Null

Else:
txtAge = DateDiff("yyyy", [DOBPET], Now()) + _
Int(Format(Now(), "mmdd") < Format([DOBPET], "mmdd"))
End Sub



:

Code and expressions will work regardless of whether or not the
control on
the form is disabled. Disabling only allows you to deny physical data entry.
What you can do is use code instead of an expression to populate the field,
something like (aircode):

Sub txtBirthDate_AfterUpdate
If DateDiff("WW", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else If DateDiff("yyyy", Me.txtBirthDate, Date) <=8 Then
Me.txtAge = Null
Else
'your date expression here
End Sub
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access

Dears,

how to disable field with conditons if false

e.g. I have a age field which populates age of a person with weeks. i.e.
one
filed the user enters date and on these dates the age populates
with weeks
e.g. 1 year 50 weeks

my question

1. if the age is 8 years or above the the date field should disable
2. if the age is 8 weeks or less the text field or date field
should
disable

hope its possible
plz help

thanks
 
G

Gerald

Cheers, Douglas for ur suggestion..

Now, this is where I stand

DOB = date of birth in date format
age = populates age in years and weeks (as entered in DOB)

if age above 8 years 0 weeks age field should display " invalid age "
if age below 8 weeks age field should display " invalid age "

can u possibly advice how best can we do this ?

Any help will be appreciated

Many thanks
 
D

Douglas J. Steele

To calculate the age in years and weeks, calculate the difference in weeks.
The years will be the integer portion of the total weeks divided by 52, the
weeks will be total weeks mod 52:

However, now that I'm looking closer at your original code, I'm getting
confused. In one case it used Me.txtBirthDate, in another it used [DOBPET].
I'll assume that you have a field on your form named txtBirthDate which is
bound to the DOB field in your table. With that assumption, you need
something like:

Private Sub txtBirthDate_AfterUpdate()

If IsDate(Me.txtBirthDate) Then
If DateDiff("ww", Me.txtBirthDate, Date) < 8 Then
Me.txtAge = "Invalid Age"
ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) > 8 Then
Me.txtAge = "Invalid Age"
Else
Me.txtAge = DateDiff("ww", Me.txtBirthDate, Date) \ 52 & _
" years, " & _
DateDiff("ww", Me.txtBirthDate, Date) Mod 52 & _
" weeks."
End If
Else
Me.txtAge = "Invalid Date of Birth"
End If

End Sub
 
G

Gerald

Douglas, u r a Star

Thanks a bunch ur help is appreciated

all works fine, except when I enter date as 01/01/1997, it shows 8 years 35
weeks

ideally it should show "Invalid date" i.e. even if it is 8 years 1 week ,
it should show "Invalid date"

any suggestion on this

Cheers!!

Douglas J. Steele said:
To calculate the age in years and weeks, calculate the difference in weeks.
The years will be the integer portion of the total weeks divided by 52, the
weeks will be total weeks mod 52:

However, now that I'm looking closer at your original code, I'm getting
confused. In one case it used Me.txtBirthDate, in another it used [DOBPET].
I'll assume that you have a field on your form named txtBirthDate which is
bound to the DOB field in your table. With that assumption, you need
something like:

Private Sub txtBirthDate_AfterUpdate()

If IsDate(Me.txtBirthDate) Then
If DateDiff("ww", Me.txtBirthDate, Date) < 8 Then
Me.txtAge = "Invalid Age"
ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) > 8 Then
Me.txtAge = "Invalid Age"
Else
Me.txtAge = DateDiff("ww", Me.txtBirthDate, Date) \ 52 & _
" years, " & _
DateDiff("ww", Me.txtBirthDate, Date) Mod 52 & _
" weeks."
End If
Else
Me.txtAge = "Invalid Date of Birth"
End If

End Sub

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Gerald said:
Cheers, Douglas for ur suggestion..

Now, this is where I stand

DOB = date of birth in date format
age = populates age in years and weeks (as entered in DOB)

if age above 8 years 0 weeks age field should display " invalid age "
if age below 8 weeks age field should display " invalid age "

can u possibly advice how best can we do this ?

Any help will be appreciated

Many thanks
 
D

Douglas J. Steele

It may be better to use

ElseIf DateDiff("ww", Me.txtBirthDate, Date) > 416 Then

(416 is 8 * 52) rather than

ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) > 8 Then

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Gerald said:
Douglas, u r a Star

Thanks a bunch ur help is appreciated

all works fine, except when I enter date as 01/01/1997, it shows 8 years
35
weeks

ideally it should show "Invalid date" i.e. even if it is 8 years 1 week ,
it should show "Invalid date"

any suggestion on this

Cheers!!

Douglas J. Steele said:
To calculate the age in years and weeks, calculate the difference in
weeks.
The years will be the integer portion of the total weeks divided by 52,
the
weeks will be total weeks mod 52:

However, now that I'm looking closer at your original code, I'm getting
confused. In one case it used Me.txtBirthDate, in another it used
[DOBPET].
I'll assume that you have a field on your form named txtBirthDate which
is
bound to the DOB field in your table. With that assumption, you need
something like:

Private Sub txtBirthDate_AfterUpdate()

If IsDate(Me.txtBirthDate) Then
If DateDiff("ww", Me.txtBirthDate, Date) < 8 Then
Me.txtAge = "Invalid Age"
ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) > 8 Then
Me.txtAge = "Invalid Age"
Else
Me.txtAge = DateDiff("ww", Me.txtBirthDate, Date) \ 52 & _
" years, " & _
DateDiff("ww", Me.txtBirthDate, Date) Mod 52 & _
" weeks."
End If
Else
Me.txtAge = "Invalid Date of Birth"
End If

End Sub

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Gerald said:
Cheers, Douglas for ur suggestion..

Now, this is where I stand

DOB = date of birth in date format
age = populates age in years and weeks (as entered in DOB)

if age above 8 years 0 weeks age field should display " invalid age "
if age below 8 weeks age field should display " invalid age "

can u possibly advice how best can we do this ?

Any help will be appreciated

Many thanks
 
G

Gerald

Thanks for ur time and for the solution
Cheers!!

Douglas J. Steele said:
It may be better to use

ElseIf DateDiff("ww", Me.txtBirthDate, Date) > 416 Then

(416 is 8 * 52) rather than

ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) > 8 Then

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Gerald said:
Douglas, u r a Star

Thanks a bunch ur help is appreciated

all works fine, except when I enter date as 01/01/1997, it shows 8 years
35
weeks

ideally it should show "Invalid date" i.e. even if it is 8 years 1 week ,
it should show "Invalid date"

any suggestion on this

Cheers!!

Douglas J. Steele said:
To calculate the age in years and weeks, calculate the difference in
weeks.
The years will be the integer portion of the total weeks divided by 52,
the
weeks will be total weeks mod 52:

However, now that I'm looking closer at your original code, I'm getting
confused. In one case it used Me.txtBirthDate, in another it used
[DOBPET].
I'll assume that you have a field on your form named txtBirthDate which
is
bound to the DOB field in your table. With that assumption, you need
something like:

Private Sub txtBirthDate_AfterUpdate()

If IsDate(Me.txtBirthDate) Then
If DateDiff("ww", Me.txtBirthDate, Date) < 8 Then
Me.txtAge = "Invalid Age"
ElseIf DateDiff("yyyy", Me.txtBirthDate, Date) > 8 Then
Me.txtAge = "Invalid Age"
Else
Me.txtAge = DateDiff("ww", Me.txtBirthDate, Date) \ 52 & _
" years, " & _
DateDiff("ww", Me.txtBirthDate, Date) Mod 52 & _
" weeks."
End If
Else
Me.txtAge = "Invalid Date of Birth"
End If

End Sub

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)



Cheers, Douglas for ur suggestion..

Now, this is where I stand

DOB = date of birth in date format
age = populates age in years and weeks (as entered in DOB)

if age above 8 years 0 weeks age field should display " invalid age "
if age below 8 weeks age field should display " invalid age "

can u possibly advice how best can we do this ?

Any help will be appreciated

Many thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top