Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2008
    Location
    All over, Now in Missouri
    Posts
    73

    Unanswered: VBA Setfocus not working

    I have a grade field that I want to error check
    - if grade is greater than 100
    -- give message
    -- set focus back that field to be corrected

    Here is what I have, but after you hit Ok, it goes to the next field and not back to problem field.

    Private Sub GSWrittenTest_Exit(Cancel As Integer)
    If GSWrittenTest > 100 Then
    MsgBox "Your Grade is above 100." _
    & vbCrLf & " " _
    & vbCrLf & "Please Try Again.", vbExclamation
    GSWrittenTest.SetFocus
    End If

    End Sub

    Peter
    We tend to look at Linear paths which can lead us to a path of resistance!

  2. #2
    Join Date
    Nov 2007
    Posts
    10
    I had a similar problem a while ago.

    There may be another way but what I did was set the focus to some other field and then back to the one I wanted.

    So something like this:

    Private Sub GSWrittenTest_Exit(Cancel As Integer)
    If GSWrittenTest > 100 Then
    MsgBox "Your Grade is above 100." _
    & vbCrLf & " " _
    & vbCrLf & "Please Try Again.", vbExclamation
    <Some Other Field>.SetFocus <---------------Add this line
    GSWrittenTest.SetFocus
    End If

    End Sub

  3. #3
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Why not just use the Validation Rule and Validation Text properties to do this?

    Just a suggestion since I see nothing in the code that these two properties couldn't handle without one bit of code
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  4. #4
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    The standard way of doing Validation for data entered into a textbox is to use the BeforeUpdate event, then use Cancel = True if the Validation fails.

    Code:
    Private Sub GSWrittenTest_BeforeUpdate(Cancel As Integer)
    If Me.GSWrittenTest > 100 Then
     MsgBox "Your Grade is above 100." & vbCrLf & " " & vbCrLf & "Please Try Again.", vbExclamation
     Cancel = True
     Me.GSWrittenTest.SelStart = 0
     Me.GSWrittenTest.SelLength = Len(Me.GSWrittenTest)
    End If
    End Sub
    If the data entered is over 100, this will automatically place the focus back in the GSWrittenTest textbox and highlight the erroneous data, ready for the user to correct their mistake.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Apr 2008
    Location
    All over, Now in Missouri
    Posts
    73
    I will play with the validation, but the last post worked perfect. Thanks for the input.

    Peter
    We tend to look at Linear paths which can lead us to a path of resistance!

  6. #6
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Glad we could help!
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  7. #7
    Join Date
    May 2011
    Posts
    1

    Thumbs up Set Focus Problem Resolved

    Just a thank you http://www.dbforums.com/db_images_v3...lies/smile.gif
    to Math Tutor.
    Jumping the focus first to a field other than the one you want focus on resolved my problem. Really helped.
    Thanks,
    jdb

Posting Permissions

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