Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2010
    Posts
    8

    Question 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.

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    What's the code you use and with which version of Access?
    Have a nice day!

  3. #3
    Join Date
    Apr 2010
    Posts
    8
    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
    End If
    End Sub

    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??

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    That's strange. Your code is valid in Access 2003 but not in Access 2007. Here is a solution that works in 2007:
    Code:
    Option Compare Database
    Option Explicit
    Private m_warning As Boolean
    
    Private Sub Text1_AfterUpdate()
    
        If Not IsNumeric(Me.Text1.Value) Then
            MsgBox "Something"
            m_warning = True
        End If
        
    End Sub
    
    Private Sub Text2_GotFocus()
    
        If m_warning = True Then
            m_warning = False
            Me.Text1.SetFocus
        End If
        
    End Sub
    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).
    Have a nice day!

  5. #5
    Join Date
    Apr 2010
    Posts
    8

    Thanks Sinndho

    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!

    Cheers

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •