Results 1 to 9 of 9
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Bizarre set focus behaviour on tab control (2000sp3)

    I have this code in the on exit event of a control on a page of a tab control:

    If (LastKey = vbKeyTab Or LastKey = vbKeyReturn) And MouseMoved = 0 Then
    If Me!Control1 = 1 Then Me!Control2.SetFocus
    End If

    Whenever the code is activated, however, the focus passes to the page, not the control. Control2 is enabled, and has no code on entry or got focus. Control1 has no other events coded. I have tried disabling the detail on mousemove event and setting form keypreview to no and commenting out the first if clause, but the behaviour remains. I have double checked the spelling of control names. There are no events coded on Page1. I have tried changing the set focus statement to:

    Me!TabCtl0.Pages("PAGE1").Controls("Control2").Set Focus

    and also tried

    DoCmd.GoToControl ("Control2")

    but every time it behaves the same.

    I can't think of anything else to try. Any thoughts?

    Thanks

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    I don't know what LastKey and MouseMoved are.

    I don't think you should do this in the OnExit event. If you want to trap the Tab or Enter key, you must use the KeyDown event and set KeyCode = 0 to prevent Access from handling these keys automatically.

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    I may be doing that part wrong, though it's worked for me on many forms before, and isn't the problem because if I strip out that if clause the result is the same.

    The most bizarre thing is that if I try and set the focus to a control on another page of the tabcontrol it works fine. I also tried doing the same moving the focus from a control on page2 to another control on page2, and that works fine as well. It's only when moving the focus between controls on Page1 that I'm having the problem (and I've tested it with other controls, and still have the same problem). I've checked, form, tabcontrol, page and control properties time and again, and can find nothing that should interfere with normal behaviour.

    It has to be on the on exit event, as I want it to happen when tabbing through a form to check as well as when entering data - so none of the update events would work. I did try putting it in the on enter event of the next control in the tab list, which fired OK, but led to exactly the same problem.

    Help! <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

    Mark

    PS, just so you know, MouseMoved is a global flag set to 1 in the onmousemove event of the forms detail, and reset to 0 in the code behind the onkeydown event of the form, which also passes the key to the global variable LastKey. The forms keypreview property is obviously set to yes.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    Thanks for the explanation of LastKey and MouseMoved. Although you put it in a PS, it is essential in order to understand what you are doing. Without knowing what these variables are, your OnExit procedure doesn't make sense.

    I created a small demo form along the lines you describe. It works as intended, so your code seems to be OK, the problem must be caused by something else.

    This is hard to analyze from a distance. Could you post a stripped-down copy of your database? Leave only the bare minimum necessary to demonstrate the problem in the copy - the problem form, of course, and perhaps a source table with one record. If the problem also occurs in an unbound form, you don't even need to include a source table. All other database objects should be deleted. Then do a Compact and Repair, and put the database in a .zip file. The zip file should be below 100 KB. Attach it to your reply.

  5. #5
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    OK, here it is. As a last resort I'd created a new form with a new tab control, and then copied all the controls and code over. So this is a slimmed down version of that form. Three new points:

    1) Leaving out the mousemove and key checking if clause, the focus jumps correctly if you exit the control with a mouse move, rather than tab or enter.
    2) A break point in the code leading to a debug sometimes ends up with the focus moving correctly
    3) Even though I disable the forms current event (which sets the focus to the id number control if it's empty), code execution sometimes passes to the Number Onenter code.

    Hope you can make some sense of it.
    Attached Files Attached Files

  6. #6
    3 Star Lounger
    Join Date
    Feb 2001
    Posts
    369
    Thanks
    2
    Thanked 1 Time in 1 Post

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    Sorry, forgot to mention, the control I've mostly been working with on page1 is question 20, control name "Work_away", and the 3 controls with data on page2 are the examples of how it works there.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    Turning off Name AutoCorrect in Tools/Options... speeds up the opening of the form (perhaps you already have that in the production version).

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    OK, I see what you mean. Very strange indeed. I tried some things, but unfortunately they didn't work. I'll have a closer look later on.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Bizarre set focus behaviour on tab control (2000sp3)

    Well, I finally found the culprit. I don't pretend to understand WHY, but when you set the focus to another control in the OnExit event procedure of some (but not all) of the combo boxes, the OnEnter event procedure for the Number control is called. This executes SendKeys "{HOME}", which results in selecting the page tab. I haven't found a pattern that explains which controls exhibit this behavior.

    The easiest solution is to do without the Number_Enter procedure. I don't see why you need it anyway.

    If you can't live without it, you can define a module-level variable at the top of the form module:

    Private SettingFocus As Boolean

    In the Number_Enter procedure, test the value of this variable:

    Private Sub Number_Enter()
    If SettingFocus = False Then
    SendKeys "{HOME}"
    End If
    End Sub

    And in the OnExit proecedures of each of the "problem" combo boxes, set the variable temporarily to True:

    Private Sub Work_away_Exit(Cancel As Integer)
    If (LastKey = vbKeyTab Or LastKey = vbKeyReturn) And MouseMoved = 0 Then
    If Me.Work_away = 1 Then
    SettingFocus = True
    [Away note].SetFocus
    SettingFocus = False
    End If
    End If
    End Sub

Posting Permissions

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