Results 1 to 10 of 10
  1. #1
    Pat1362 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Posts
    4

    Returning Focus to a textbox on an access form using On Exit Event

    I have recently returned to programming in MS Access after almost 13 years. I am in the process of creating a small membership application and have come up agains the following problem.



    I am coding a form to allow the user add a new member to the database. I have a text box on the form (txtMemNo) where user enters the Membership Number and I'm using the OnExit event of the text box to validate the entry. The validation works fine, my problem is that when it fails I want to return the focus to txtMemNo but my code below doesn't work.

    Code:
    Private Sub txtMemNo_Exit(Cancel As Integer)
    Dim strNumber As String
    Dim blnNumber As Boolean
    If txtMemNo.Text <> "" Then 'check that something has been entered in textbox
    strNumber = txtMemNo.Value 'populate the string variable
    blnNumber = IsNumeric(strNumber) 'numeric check
        If blnNumber = True Then
        MsgBox ("Checking if membership number already exists")
        'Code here to call routine to check if membership number exists
        Else
        MsgBox ("Please enter a number") 'Error message
        
        txtMemNo.Text = "" 'clear text
        txtMemNo.SetFocus 'return cursor to text box
        End If
    End If
    End Sub
    If I tab out of txtMemNo the focus moves to the text box that is next in tab order and if I click out of it focus remains in the textbox I click into. I'd be grateful for any advice on how to fix this. TIA.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,537
    The "BeforeUpdate" would be a better place for validation code. If the validation fails the update can be cancelled.

    See: https://docs.microsoft.com/en-us/off...reupdate-event
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Pat1362 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Posts
    4
    Hi Bob,

    Both the form and the textbox are unbound so cancelling an update is not the issue. My problem is returning focus to the text box when validation fails.

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,537
    Quote Originally Posted by Pat1362 View Post
    Hi Bob,

    Both the form and the textbox are unbound so cancelling an update is not the issue. My problem is returning focus to the text box when validation fails.
    The BeforeUpdate event is the best place to handle validation. Try something like:
    Code:
    If Not IsNumeric(Me.ActiveControl) Then    
        MsgBox ("Please enter a numeric value")
        Cancel = True
    End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,537
    If you use the BeforeUpdate event and cancel if the validation fails, then the focus never leaves the control.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,789
    Agree with Bob. After LostFocus event for controls happens after Exit, so you're setting focus for 1 microsecond in Exit code and then LostFocus occurs.
    Two things I'd rethink:
    - making users dismiss the message box, especially since the check cannot happen until it's dismissed. Just run it an message if there's a problem
    - IsNumeric(12a) should raise an error and you have no error handler. IsNumeric(a12) will return True. There's really no need for this test. Use DLookup for whatever value is entered, which will return a valid value or not. It's really that simple I think.
    Last edited by Micron; 04-15-2021 at 08:23 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Pat1362 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Posts
    4
    Ok that part I can understand. If I place the code you posted above in the BeforeUpdate event I get a compile error saying that I've "used an End If without a block If" ??

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,789
    Post your code. Please use code tags (# on posting toolbar) and indentation. Or look for which END IF has no IF and fix that. Could also happen with nested blocks such as IF nested in WITH.
    Last edited by Micron; 04-15-2021 at 08:36 AM. Reason: correction
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Pat1362 is offline Novice
    Windows 10 Office 365
    Join Date
    Apr 2021
    Posts
    4
    Hi Micron, my comment about compile error was not in reference to my own code but the code Bob had posted. I have a good understanding of basic coding principles as I worked as a database programmer on an IBM AS400 system for many years. I'm just a little rusty on vba especially with regard to the sequence in which control events happen as I haven't used it for such a long time. Anyway I have decided to implement the validation when the user clicks an update button as it's easier to set the focus at that point. Thanks to both you and Bob for taking the time to reply to my post.

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,789
    I saw nothing wrong with the If / End If part of his code so I figured your error had something to do with how you incorporated it. Glad you solved it, and post back if you need anything else.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-25-2019, 09:55 AM
  2. Replies: 1
    Last Post: 12-23-2015, 08:45 AM
  3. Help with Validation code on exit event on a form
    By thebionicredneck2003 in forum Programming
    Replies: 2
    Last Post: 05-17-2013, 06:04 AM
  4. How do I capture Access DB exit event
    By jscriptor09 in forum Access
    Replies: 3
    Last Post: 09-21-2011, 09:45 AM
  5. Any change of focus on form Event?
    By trb5016 in forum Forms
    Replies: 1
    Last Post: 02-11-2011, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums