Consulting

Results 1 to 9 of 9

Thread: SetFocus with Frames not Working

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    15
    Location

    SetFocus with Frames not Working

    Hi,

    I have a UserForm with several text boxes embedded into frames (used only as
    a visual aid for grouping the text boxes).

    Problem is, when the text boxes are embedded within a Frame, the SetFocus
    doesn't work & the next textbox that is outside the frame is selected. I want it so when I press enter the next textbox in the frame would be selected.

    Does anyone have any idea how to fix this?

    Thanks in advance.

    Here is a link to the same post in another forum:https://www.mrexcel.com/board/thread...rking.1129772/

  2. #2
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,167
    Location
    Set the TabIndex of the Frame next after the TabIndex of the last control before the Frame is used, then set the TabIndexes of the controls in the Frame. When the previous Control is Exited, the first control in the Frame will get its Focus set. When the last control in the Frame is Exited the next TabIndexed control on the UserForm will get the focus.

    IOW, Frames are TabIndexed in order with all other Controls on the UserForm. Controls in Frames are TabIndexed separately from On-UserForm Controls.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Apr 2018
    Posts
    15
    Location
    Hi SamT, thank you for your quick response.

    Either I haven't done what you said correctly or I just don't understand. I'm not very good with Excel, I'm just a beginner.
    I have two frames, both with multiple textboxes, one frame has a TabIndex of 0 and the other a TabIndex of 1.
    In each frame I have multiple Textboxes that have a TabInex sequence going from 0,1,2,3,4...15 and onward.
    I also have multiple images and commandbuttons in the frames that also have a TabIndex.

    I want it so that in the frame with the TabIndex of 0, after I press enter with the cursor in the textbox that has a TabIndex of 10, the SetCursor would then jump to the second frame into the Textbox that has a TabIndex of 0.

    Thanks again.

  4. #4
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,167
    Location
    TabStop is your friend. If the TabStop property of a Control is set False, that control will not be used by the Tab key and Enter key methods of Exiting a Control.


    Set the TabStop property of all controls (in Frame: TabUndex=0) (with a TabIndex greater than 10) to False. Then when you Exit the Textbox with a TabIndex of 10, the Frame will be Exited and the Next TabIndex on the Form (the Control in Frame: TabIndex=1 with the lowest TabIndex) will recieve the Focus. This assume that you already have the TabStop of Frame: TabIndex=1 set True.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Regular
    Joined
    Apr 2018
    Posts
    15
    Location
    Sorry for the late response, but I have tried to do what you had told me to do, but it still doesn't work for me.

    I'm not sure if this is the problem, but I do have images, commandbuttons and listboxes in the frames as well, so I think they are messing with the TabIndex, so it won't let my SetCursor to jump to the next frame.

    Is there any/other fix to this?

  6. #6
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,167
    Location
    Yeah, getting User Work-Flow to work using only TabIndex can be hard, you can skip around with
    Private Sub SomeControl_Exit(ByVal Cancel As MSForms.ReturnBoolean)
       Cancel = True
       OtherControl.SetFocus
    End Sub
    Think of it as GoTo for TabIndex, so use it sparingly.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    VBAX Regular
    Joined
    Apr 2018
    Posts
    15
    Location
    I used your code, and it did work, but every time I close out of the app, an error shows up.

    This is the error that pops up:
    err.png

    And here is where the error message shows in the code:
    err1.png

    Is there a fix to this?
    Also, why do I have to use this code sparingly?
    And is there a way that exists to do this without needing to use a code? Because I did what you told me to do with making a userform with only textboxes and it did work for me, so is there any way to go around this problem without needing VBA?

  8. #8
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,167
    Location
    Anybody?

    My store of patience has been seriously depleted by the panic arisen from the newest flu virus.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,017
    Location
    Can you upload your workbook (or a copy with any sensitive data removed)? I'll give it a bash from my 'lockdown' office.
    Semper in excretia sumus; solum profundum variat.

Posting Permissions

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