Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date validation

Status
Not open for further replies.

cabbey77

Technical User
Aug 6, 2004
68
US
Okay, I have a date field in a form from a single table that needs to be "required". I have tried: IS Not Null, <>0 and <> "" in the validation rule of the text box. These do not work. I have also placed the following code in the Before Update and Lost Focus events:

Private Sub Request_Recvd_Dt_LostFocus()

If Not IsDate(Me!Request_Recvd_Dt) Then
MsgBox "You must enter a date!"
Cancel = True
Me.Undo
Me!Request_Recvd_Dt.SetFocus
End If
End Sub

Both of these work about half way. After clicking OK in the Message Box, you can just move right past the field and proceed to save the form. The SetFocus line does not work at all with or without the Me!

I have also tried adding this code to my Save and Close command button.

Any ideas?
 
Don't know why, perhaps someone else can provide the reason, but the answer to your problem is to setfocus to another control then back to Request_Recvd_Dt. This accomplishes your task!

Code:
Private Sub Request_Recvd_Dt_LostFocus()

   If Not IsDate(Me!Request_Recvd_Dt) Then
      MsgBox "You must enter a date!"
      Cancel = True
      Me.Undo
      [b]Me![i]AnotherControlName[/i].SetFocus[/b] 
      Me!Request_Recvd_Dt.SetFocus
    End If
End Sub

Just replace AnotherControlName with the name of a control from your form.

The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
Thank you!

It works. Knowing why would be nice, but the big question, is this a consistent issue, and is this a consistent work around?
 
Knowing why would be nice, and yes, it's a problem that I've encountered a number of times on Lost Focus! It appears that when exiting the textbox focus is kind of betwixt and between, and setting focus first to another control and then back works. Sometimes, if I don't have a convenient control to use for this move, I'll use a "Phantom" textbox that I shrink until it's the size of a period, then position down in a corner, where it's all but invisible. The I'll set the focus to it, then back to the desired textbox.

The Missinglinq

There's ALWAYS more than one way to skin a cat!
 
How are ya cabbey . . .

As far as I can see, you already have what yo need! Consider the following in relation to [blue]Me.Undo[/blue]:
[ol][li][blue]Me.Undo[/blue] is the same as hitting the [blue]ESC[/blue] key . . . you remove the data entry and if this was the first edited field [blue]you take the record out of edit mode![/blue] Either way since its the same as if no data was entered, of course its easy to just skip by![/li]
[li]If your using the [blue]BeforeUpdate[/blue] event of the control . . . [purple]the focus retains its position![/purple] and [blue]its not necessary to set focus![/blue][/li][/ol]
The above dictates that if you were using the [blue]BeforeUpdate[/blue] event of the control you should have:
Code:
[blue]   If Not IsDate(Me!Request_Recvd_Dt) Then
      MsgBox "You must enter a date!"
      Cancel = True
   End if[/blue]
However . . . the [blue]required[/blue] condition you seek according to the above doesn't take hold if the user enters no data!

Welcome the [blue]BeforeUpdate[/blue] event of the form! Which is where the code I've cited should go! This is the final event you can use before a record is saved and is where you can allow the user freedom of data entry before final validation . . .

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
the required condition you seek according to the above doesn't take hold if the user enters no data!"

No data in that text box, or no data at all?

Nice info on the Me!Undo, that level of detail will certainly help me use these items more efficiently and appropriately!

Thanks for everything!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top