Unanswered: Can't get SetFocus to work as I expect
I'm a retired IT Director that hasn't programmed in Access for over 10 years! I'm in Ethiopia as a Peace Corps volunteer and am writing a small application to help a hospital track their patient records. It was all manual until now!!
The only problem I am having is being able to set the focus back to a text box on an entry form after opening a form with a validation message and then returning to the entry form. The setfocus acts as though it is totally ignored. The focus goes to the next tab item. It isn't a show stopper but sure would like to find a solution to force the focus back to a field with improper data.
Sorry, I meant to say Access 2007. I misspoke as well, the code:
Private Sub Registration_Date_AfterUpdate()
If [Registration Date] > IsToday Then
MsgBox ("The Registration Date Can't Be Before Today's Date")
Forms![New Patient Entry Form]![Registration Date].SetFocus
is executed on the AfterUpdate action in the field I am validating. So I never leave the focus from the entry screen. The Ethiopian Calendar has 13 months, 12 months with 30 days each and one mone with 5 or 6 days depending on leap year. I can't use a date field because of this. The fields are text. I tried it without the Forms!...,just the field, but that made no difference. Suggestions??
That's strange. Your code is valid in Access 2003 but not in Access 2007. Here is a solution that works in 2007:
Option Compare Database
Private m_warning As Boolean
Private Sub Text1_AfterUpdate()
If Not IsNumeric(Me.Text1.Value) Then
m_warning = True
Private Sub Text2_GotFocus()
If m_warning = True Then
m_warning = False
Here Text2 is the next control in the tab order (i.e., the control that receives the focus next). I tested the value of Text1 for a numeric value in my example but you can of course test for whatever you want (a special date format in your case).
Thanks Sinndho, That procedure worked. Since I am an older person that hasn't used Access for a long time I assumed I was doing something wrong! It is good to know it wasn't me and as the saying goes, there is more than one way to skin a cat. I appreciate your response!