Solved

Access 2010 - validation of unbound text box

Posted on 2013-05-19
10
2,506 Views
Last Modified: 2013-05-20
I have an unbound form, with an unbound text box, with format short date.

If the user enters an invalid date Access gives 'wrong type' error messsage.  I want to do my own validation and give my own 'nicer' message.

I tried to validate in the text box lost focus event, but this is too late - Access error message came first.  I then tried in the text box before update event, but again too late.

How can I catch the error myself?

(Of course, all is OK if the user uses the date picker, but not if they enter directly into the text box.)
0
Comment
Question by:MonkeyPie
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39180298
Use the textbox's ValidationRule property.
Set the property of textbox, ValidationText, to you custom message.

/gustav
0
 

Author Comment

by:MonkeyPie
ID: 39180511
Thank Gustav but no good.  I added isDate([txtEndDate]) to text box property validation rule, and "Not a valid date." to validation text.  But still, I get the "The value you entered isn't valid for this field." error generated by access checking the text box format of short date.

So, any further ideas?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39180705
Yes, you may need to remove formatting and normal validation from the textbox.
Then apply this code:

Private Sub txtEndDate_AfterUpdate()

    With Me!txtEndDate
        .Value = Format(.Value, "Short Date")
    End With

End Sub

Private Sub txtEndDate_BeforeUpdate(Cancel As Integer)

    Dim strValue    As String
   
    strValue = Nz(Me!txtEndDate.Value)
   
    If strValue <> "" Then
        Cancel = Not IsDate(strValue)
    End If
   
    If Cancel Then
        MsgBox "Please provide a date.", vbExclamation + vbOKOnly, "End Date"
    End If
   
End Sub

/gustav
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39180716
I prefer to use some other event associated with the form to do my validation in VBA.

What else do you have going on on that form?  Is there a "save" button or some other button that will cause a particular action (filter, report, ...).  If so, you could put your "validation rule" in VBA code in the click event of that button and if the "validation" fails, exit the sub.

Another place that I generally put my validation rules for bound fields (would work for unbound as well) is in the Form_BeforeUpdate event.  With the BeforeUpdate event, you would simply set the Cancel argument to True if the "validation" fails.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39180935
The issue is that Access - before anything else - validates the input against the data type that matches a defined Format of the textbox. If this fails, it pops the  'wrong type' error messsage.

The trick is to remove such format. Then text is expected, and every input matches this.

/gustav
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39182496
Since there is a built-in Date Picker in Access 2010, you could just let the user select a date from the little pop-up calendar; that assures that the date will be valid.  The selected format will be applied when the user tabs away from the control.
0
 

Author Comment

by:MonkeyPie
ID: 39182760
Thank you everyone for worthwhile suggestions, however the field must have format SHORT DATE to provide the date picker, and the user must be able to type in manually their date - this is a client specification.  

When user types an invalid date we get the Access error message.

The client has drawn my attention to this 'bug' and wants it fixed.

So far, LOST FOCUS, BEFORE UPDATE, FORM SAVE BUTTON events are all too late.

So, back to the drawing board?

Some background: the form is just a little popup with START DATE and END DATE fields that are kept as registry keys as default date range for all reports.  User can change these defaults via this form.
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 39182958
I think someone may have already mentioned this, but use the Form_Error Event.

Private Sub Form_Error(DataErr As Integer, Response As Integer)
   
    If DataErr = 2113 Then
        MsgBox "Invalid date! " & DataErr
        Response = acDataErrContinue
    Else
        Response = acDataErrDisplay
    End If

End Sub
0
 

Author Closing Comment

by:MonkeyPie
ID: 39183195
Yes!  That is the solution - thank you.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39183472
Yes, that is useful here.

Just remember that the error will pop for any invalid input, so for a "normal" form you would have to check for which control is active to customise the error message.

/gustav
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now