Prevent entering letters in date field

mcdhappy80

Registered User.
Local time
Today, 17:52
Joined
Jun 22, 2009
Messages
347
Is there a way to prevent user from entering letters in date field text controls (and thus invoking the default access error message)?
I tried using something like this but it doesn't work, the default message still appears:
Code:
Private Sub dtePocetakRadnogOdnosa_BeforeUpdate(Cancel As Integer)

If IsNull(Me.dtePocetakRadnogOdnosa) Then
    MsgBox ("Morate uneti vrednost u polje Radi Od")
    Cancel = True
Else
    If IsDate(Me.dtePocetakRadnogOdnosa) Then
        If Me.dtePocetakRadnogOdnosa > Now() Then
            MsgBox ("Datum zaposlenja mora biti danasnji ili u proslosti!!")
            Me.Undo
            Cancel = True
        End If
    Else
        MsgBox "Not a date"
        Me.Undo
        Cancel = True
    End If
End If

End Sub
Thank You.
 
A date can contain letters, i.e. 01-Jan-2009 will be converted to 01-01-2009
 
A date can contain letters, i.e. 01-Jan-2009 will be converted to 01-01-2009
Ok, You're right, but what if this is the only thing what the user enters "aagshdsjsja"?
Do I check if he entered date, and prevent this message from poping?

defaulterror.jpg


Thank You
 
I went through this process not long ago. One way and the way I ended up using for all my data controls was the following.

The date control property "FORMAT" is set. In my case SHORTDATE

If a date is not entered as you know you get an Access error which is not so helpful, so I capture this error and give my own message. The place to capture it is in the Forms OnError event

You should check the dataErr number is correct for your purposes
After the error is raised you need to decide what to do with the input for example you could use a "Me.Undo"

Code:
Dim ctl_currentcontrol As Control
   
Set ctl_currentcontrol = Screen.ActiveControl

If DataErr = 2279 Or DataErr = 2113 Then
    Select Case ctl_currentcontrol.Name
        Case "YourControlName"
            MsgBox "Were you paying attention at school when dates were being explained", vbCritical, "Idiot Proofing"

'add further cases for each of your controls
    End Select
 response = acDataErrContinue
Exit Sub
End If




You could also help the user with an inputmask as well if you like.
 
Personally, I have to say, if someone tries to put in "aagshdsjsja" into a date field, they deserve to have some indecipherable error message. Sometimes you can't "idiot proof" things as it becomes just too much work for not many returns.
 
Thank You for the answer Darbid, I will try Your example.

@Bob:
What can I say I'm a perfectionist :) but I have to agree with You on this, I already lost too much time to solve this.
 
Why not use a simple input mask like: 99/99/0000;0;_
Or something simular, that way the field will not allow anything but numbers... Wont give an error if they try, but wont accept either

Also the error "Value you entered isn't valid for this field" would seem pretty self explanatory.... Not M$ worst message by far !
 

Users who are viewing this thread

Back
Top Bottom