Prevent Old Dates From being entered.

  • Thread starter Rice via AccessMonster.com
  • Start date
R

Rice via AccessMonster.com

Hello,

I need help creating a constraint that will prevent the user from enter a
date from last week or a date a week ahead into my form. I'm using Access
2003 and vba I'm not sure where to begin... any suggestions.. I was thinking
I would put something in the onchange. Help
 
L

Linq Adams via AccessMonster.com

prevent the user from enter a date from last week or a date a week ahead into my >form

It would help us to have a little clearer picture of the dates that are
acceptable, in relation to the current date. For instance, do you only want a
date from the current week at the time the date is entered?
 
R

Rice via AccessMonster.com

Oh! Yes. The user can not enter anything ahead of the current date 9/8/2008
and can only use dates from two weeks back. Would this just simple be an if
statment on the onchange event???
So the only valid dates would be Aug 25 to current.
 
J

John W. Vinson

Oh! Yes. The user can not enter anything ahead of the current date 9/8/2008
and can only use dates from two weeks back. Would this just simple be an if
statment on the onchange event???

The Change event won't work as you expect: it fires *at every keystroke* in
the textbox. Use the textbox's BeforeUpdate event instead:

Private Sub txtMyTextbox_BeforeUpdate(Cancel as Integer)
If Me!txtMyTextbox < DateAdd("ww", -2, Date()) Then
Cancel = True
MsgBox "The date cannot be older than two weeks ago", vbOKOnly
End IF
If Me!txtMyTextbox > Date() Then
Cancel = True
MsgBox "The date cannot be in the future", vbOKOnly
End If
End Sub
 
R

Rice via AccessMonster.com

Thank you, that was exactly what I needed!
The Change event won't work as you expect: it fires *at every keystroke* in
the textbox. Use the textbox's BeforeUpdate event instead:

Private Sub txtMyTextbox_BeforeUpdate(Cancel as Integer)
If Me!txtMyTextbox < DateAdd("ww", -2, Date()) Then
Cancel = True
MsgBox "The date cannot be older than two weeks ago", vbOKOnly
End IF
If Me!txtMyTextbox > Date() Then
Cancel = True
MsgBox "The date cannot be in the future", vbOKOnly
End If
End Sub
 
R

Rice via AccessMonster.com

Hello, It's me again. I've entered the code and the date function isn't
working. The date comes back with a value of null. I've checked my
references and they are all their and I tried to use now but that does't work
well in the second if statement.
Thank you, that was exactly what I needed!
[quoted text clipped - 13 lines]
End If
End Sub
 
J

John W. Vinson

Hello, It's me again. I've entered the code and the date function isn't
working. The date comes back with a value of null. I've checked my
references and they are all their and I tried to use now but that does't work
well in the second if statement.

Do you have any field or control in your database named Date? If so, Access is
probably getting confused as to whether you mean the control or the builtin
function. Rename your field or control and see if that helps.

If you don't, please open your query in SQL view and post it here. If need be
you can use Datevalue(Now()) to simulate Date(), but it should NOT be
necessary to do so.
 
R

Rice via AccessMonster.com

You are awesome!!! I did have a field called date...I want to be just like
you when grow up...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