Results 1 to 14 of 14
  1. #1
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Next Control has focus on return from another form (Access XP)

    I have a Main Menu form with about 9 controls and associated buttons.
    Each of these controls will open a another form (with the criteria that had been entered in the control). When the called form returns to the Main Menu the cursor goes to the next control.
    How can I make it go back to the same control?

    eg.
    I have 2 controls (say PO and PARTS) on the Main Menu form. If I enter a PO number in the PO control and tap enter, the code behind the PO control (AfterUpdate event) calls the PO form.
    When the PO form is closed the cursor returns to the Main Menu and settles on the PARTS control. What I want it to do is to settle back on the PO control in the Main Menu form.

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

    Re: Next Control has focus on return from another form (Access XP)

    Access handles tab and enter at a very early stage. If you want to prevent either of them from shifting the focus, you must use keyboard events at the form level.

    You could do something like this:
    - Set the Key Preview property of the form to Yes
    - Create an On Key Down event handler for the form:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab, vbKeyReturn
    KeyCode = 255
    End Select
    End Sub

    - Also create an On Key Down event handler for each text box to replace the After Update event handler

    Private Sub txtCategory_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case 255
    DoCmd.OpenForm "frmSomething", , , "CategoryID=" & Me.txtCategory.Text
    End Select
    End Sub

    - Note that I used the Text property. Since you don't leave the text box, its Value property hasn't been updated, so you must use the Text property.

    As you see, it is rather artificial.

  3. #3
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Next Control has focus on return from another form (Access XP)

    Yes it is artificial, but then the user wants it that way.
    I have changed the database to NOT use macros, so I substituted VBA code for macros. The funny things is, is that the thing would run as required with macros, ie. it would return to the same control and also leave the control blank.

    What I did with yours was to move a NULL value into the control after the OpenForm command and this seems to work OK. What I need to do with the online database is to introduce the code for each text box and comment out the previous code (AfterUpdate event) for each text box (of which there a are 9).

    Any other ideas?

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Next Control has focus on return from another form (Access XP)

    A non-urgent follow-up question: I have a number of forms where I similarly hijack the Enter key (both in textboxes and comboboxes). But I've just used the KeyDown event of the textbox (or combobox), and haven't done anything with the form-level keyboard events.

    My usual approach (as applied to your example) would be:

    Private Sub txtCategory_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyTab, vbKeyReturn
    DoCmd.OpenForm "frmSomething", , , "CategoryID=" & Me.txtCategory.Text
    KeyCode = 0
    End Select
    End Sub

    Where I've done this, it seems to be the case that setting the KeyCode to 0 prevents any change of focus. What's the rationale for also bringing the form-level event-handler into play?

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

    Re: Next Control has focus on return from another form (Access XP)

    > Any other ideas?

    For what?

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

    Re: Next Control has focus on return from another form (Access XP)

    Hmm, maybe my memory is going. I thought that wouldn't work, but perhaps that was in Access 97. In Access 2002 the way you describe works OK.

  7. #7
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Next Control has focus on return from another form (Access XP)

    I thought you may have had another way to do this.

    I'm surprised you haven't <img src=/S/grin.gif border=0 alt=grin width=15 height=15> or are you just kidding around. <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

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

    Re: Next Control has focus on return from another form (Access XP)

    I don't know if a combo box would be possible for your situation - the After Update event of a combo box fires when the user selects another value from the list, without moving the focus.

  9. #9
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Next Control has focus on return from another form (Access XP)

    I'll go with the idea you first suggested, although I will use st3333ve's suggestion as it is simpler.

    I have other controls on this form that are ComboBoxes and will have to do this with them as well.

    Thanks again.

  10. #10
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Next Control has focus on return from another form (Access XP)

    There's a problem using the text property, it has the format included in it.

    Is there another property that shows the field unformatted?

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

    Re: Next Control has focus on return from another form (Access XP)

    As I wrote before, use a combo box if possible. The After Update event of a combo box occurs if the user selects an item from the list.

    If you must use a text box, I fear you have painted yourself into a corner - if you intercept and cancel the Enter key, the Before Update and After Update events won't occur, so the Value property will still be the old value, while Text will hold the current value. There is no unformatted new value yet, since you haven't given Access a chance to update it.

  12. #12
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Next Control has focus on return from another form (Access XP)

    The combo boxes seem to be ok, as I select from the list and I cannot trap a KeyDown in this case, or can I?

    The text boxes were a challenge, but I have come up with a workaound.
    Before I start the form off witha DoCmd.Open Report, I replace the - or * format characters with an empty string in the Criteria key.
    I had to write a Replace function as the client has Access 2000. It works very well.

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

    Re: Next Control has focus on return from another form (Access XP)

    I think you can trap On Key Down for a combo box too, but I don't see the need for it.

  14. #14
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Next Control has focus on return from another form (Access XP)

    I agree with you. After having tried the combo box and called the appropriate form and returned from that form, the cursor is still in the combo box.

    This is what the user wants, so all is well.

Posting Permissions

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