Solved

MS Access Text Box Custom Error Message

Posted on 2007-12-06
8
3,037 Views
Last Modified: 2013-11-28
I have a text box called Txt_Date_Opened.

Rather than have the standard error message that if a use does not enter the proper date I would like to prompt with a more helpful message such as,  "Please enter a valid date in the format mm/dd/yyyy".

I've placed the following code in both the On Enter and On Exit but it doesn't work.


On Error GoTo Dt_Error
 

  Exit Sub
 

 Dt_Error:
 

  MsbBox "Please enter the date in the format mm/dd/yyyy", , "Attention"

  Exit Sub

Open in new window

0
Comment
Question by:TylerDerden
  • 5
  • 3
8 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Private Sub Txt_Date_Opened_BeforeUpdate(Cancel as Integer)

If Not IsDate(Nz(Me.Txt_Date_Opened, "foo")) then
   msgbox "Please enter a valid date in the format mm/dd/yyyy"
   Cancel = True
   Docmd.CancelEvent  'May need this line too
Else
   'Good to go.
End If
 
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>On Enter and On Exit
On enter fires before the user enters a value, and On Exit is not cancellable via VBA code if the user enters a value that fails validation.  
BeforeUpdate fires after a value is entered, before focus moves on to the next control, and is cancellable via VBA code.

0
 

Author Comment

by:TylerDerden
Comment Utility
Thnx. I  think we r close. I made one slight change to the if statement. The code works if I leave the field blank but if I put in a bad value such as the text gd I still get the standard MS Access error.

Private Sub Txt_Date_Opened_BeforeUpdate(Cancel As Integer)

 If Not IsDate(Nz(Me.Txt_Date_Opened, "foo")) Then

   MsgBox "Please enter a valid date in the format mm/dd/yyyy"

   Cancel = True

   DoCmd.CancelEvent  'May need this line too

 End If
 

End Sub

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
You'll want to step-through this code and see how it is executing.  Try this, then run, type sb in the combo box.  
You should be able to mouse over Me.txt_date_opened (make sure there's no typos) and make sure it contains the value sb.



Private Sub Txt_Date_Opened_BeforeUpdate(Cancel As Integer)
STOP   '<--- ADD THIS
 If Not IsDate(Nz(Me.Txt_Date_Opened, "foo")) Then
   MsgBox "Please enter a valid date in the format mm/dd/yyyy"
   Cancel = True
   DoCmd.CancelEvent  'May need this line too
 End If
 
End Sub
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:TylerDerden
Comment Utility
I added the line of code. The default value is todays date.

If I delete the default value the subroutine works fine.
I step into the line of code.
The value of null passes to the code and I get the prompt.

However, if I add in text such as sb or a number then I do not step into the code at all. Rather the MS Access default error message populates, "The value you entered isn't valid for this field."

Can I turn off MS Access error handling then turn it back on. Kind of like turning off  setwarnings?
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
Comment Utility
In that case, your table.column data type is forcing the formatting before you can trap it with the form.control.BeforeUpdate event.

You'll likely need to make your textbox unbound, ie nothing in the ControlSource property.  This will allow any value to be entered.
Then, you can always have the textbox with the correct ControlSource hidden, and if validation is passed set Me.UserEnteredControl = Me.TheBoundCountrol

>Can I turn off MS Access error handling then turn it back on.
Not in this case.
0
 

Author Comment

by:TylerDerden
Comment Utility
Thanks that works great!
Final code is attached.
Private Sub Txt_Date_Opened_BeforeUpdate(Cancel As Integer)
 

If Not IsDate(Nz(Me.Txt_Date_Opened, "foo")) Then

   MsgBox "Please enter a valid date in the format mm/dd/yyyy"

   Cancel = True

   DoCmd.CancelEvent

Else: Me.Txt_Dt_Opened_Bound = Me.Txt_Date_Opened

End If
 

End Sub

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

10 Experts available now in Live!

Get 1:1 Help Now