Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2005
    Location
    Charleston, SC
    Posts
    8

    Unanswered: .SetFocus Problems

    Is there anyreason why I can't setfocus on a form field. Here is the code:

    Code:
    Sub CheckSerialLen()
    'Checks length of serial number
         If Len(Me.SerialNumber.Text) < 10 Then
            Me!SerialNumber.SetFocus
            MsgBox "Serial Number Length is Incorrect.  Please Check Number Again."
         End If
    End Sub
    Regardless of where I put the "Me!SerialNumber.SetFocus" in this sub it will not set the focus on it. Any suggestions?

  2. #2
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Does the message box fire?

    Have you tried Me.SerialNumber.Setfocus?
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  3. #3
    Join Date
    Oct 2005
    Location
    Charleston, SC
    Posts
    8
    Yes, the message box comes up and then it goes right on to the next field. I think I've tried everything. I had to stand up and walk away for an hour, but I still can't get it to work.

  4. #4
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Where are you calling the sub from?
    Paul

  5. #5
    Join Date
    Oct 2005
    Location
    Charleston, SC
    Posts
    8
    That sub came directly from the code of the form. Si that not a good place for it? I'm really new to VBA. I normally write asp.

  6. #6
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    What I mean is that the sub you posted doesn't look like a normal "event" sub, which would look like

    Private Sub cmdCarOut_Click()

    for a button click event or

    Private Sub txtCompany_AfterUpdate()

    for the after update event of a textbox. Therefore, I assume that's a sub that you call from some other event or macro or whatever. I wondered where that was, as it could be relevant.
    Paul

  7. #7
    Join Date
    Oct 2005
    Location
    Charleston, SC
    Posts
    8
    Code:
    Private Sub SerialNumber_LostFocus()
    'Calls Check Serial
    CheckSerialLen
    End Sub
    
    
    Sub CheckSerialLen()
    'Checks length of serial number
         If Len(Me.SerialNumber.Text) < 10 Then
            Me!SerialNumber.SetFocus
            MsgBox "Serial Number Length is Incorrect.  Please Check Number Again."
         End If
    End Sub
    Is that what you are referring to?

  8. #8
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    That's what I suspected. When you try to set focus to a control during it's own lost focus event, it doesn't work. I suspect that since the control still has the focus while the code runs, your code doesn't really change anything, so when the code is done Access does what it would have done anyway, which is move focus to the next control. Try setting focus to any other control right before, like:

    Me!SomeOtherControl.SetFocus
    Me!SerialNumber.SetFocus
    Paul

  9. #9
    Join Date
    Oct 2005
    Location
    Charleston, SC
    Posts
    8
    Thnx, that worked.

Posting Permissions

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