PDA

View Full Version : SetFocus with Frames not Working



jarekmos
04-06-2020, 07:55 AM
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/threads/setfocus-with-frames-not-working.1129772/

SamT
04-06-2020, 01:08 PM
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.

jarekmos
04-08-2020, 08:43 AM
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.

SamT
04-08-2020, 11:37 AM
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.

jarekmos
04-13-2020, 11:31 AM
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?

SamT
04-13-2020, 04:05 PM
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.

jarekmos
04-13-2020, 05:43 PM
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:
26324

And here is where the error message shows in the code:
26325

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?

SamT
04-14-2020, 02:19 PM
Anybody?

My store of patience has been seriously depleted by the panic arisen from the newest flu virus.

paulked
04-14-2020, 03:23 PM
Can you upload your workbook (or a copy with any sensitive data removed)? I'll give it a bash from my 'lockdown' office.

melpa
06-08-2020, 04:16 AM
Hi jarekmos,
I'm a beginner too.
Did you know about View Tab Order?
In the VB Editor, with the UserForm selected, find View in the toolbar, then Tab Order.
There you can manually arrange the tab order - you can select one or more things in the list and move them up or down.
First select the whole userform and make sure the frames - and anything outside them - are in the right order, then each frame to check the order inside them. The order of the inactive labels doesn't seem to matter as their TabStop is automatically set to False.