PDA

View Full Version : [SOLVED:] Setfocus on Nested Multipage(s)



kestrel1306
06-09-2017, 10:28 PM
Hi guys,

Due to poor performance graphics at work I have had to change from a continous userform to multipages with some nested multipages.

For data validation I need to check if certain items have been checked or not, and if not then send the user back to them for further attention.

If the item is just on a multipage, then it's simple to do using

UserForm8.multipage1.Value = 4
UserForm8.control1.SetFocus

The problem arises where I need to go a control on a multipage that's on a multipage.

I would have hoped that the following would have worked, but alas no...

UserForm8.multipage1.Value = 4 'upper page - 5th tab
UserForm8.multipage2.Value = 0 'lower page - 1st tab
UserForm8.control1.SetFocus

Any help would be greatly appreciated thanks.


Regards
Kes

gmayor
06-09-2017, 11:11 PM
Without the userform to check it is difficult to be sure, but where TextBox1 is the first control on the first tab of MultiPage2 (which is a multipage form on the fifth tab of MultiPage1) the following certainly works for me.


MultiPage1.Value = 4
MultiPage2.Value = 0
TextBox1.SetFocus

kestrel1306
06-10-2017, 12:55 AM
Hi gmayor,

Gosh what an idiot I am.

I was ultimately referencing a radio button inside a frame, which was causing the fault.

After you said it worked I played with it and got it work by referring to the frame.

Thanks very much for the quick reply and guidance.


Regards
Kes

gmaxey
06-10-2017, 12:34 PM
You can also do this dynamically:


Private Sub UserForm_Initialize()
'The textbox of interest located on Page 0 of Multipage2 nexted on Page 0 of Multipage1.
'Hide it
MultiPage1.Value = 1
MultiPage2.Value = 1
DynamicSetFocus TextBox3
lbl_Exit:
Exit Sub
End Sub
Sub DynamicSetFocus(oCtrl As Control)
Dim oParentCtrl As Object
Dim arrCtrl()
Dim lngParent As Long, lngIndex As Long
Set oParentCtrl = oCtrl.Parent
If oParentCtrl.Name = Name Then
oCtrl.SetFocus
Else
Do Until oParentCtrl.Name = Name
ReDim Preserve arrCtrl(1, lngParent)
arrCtrl(0, lngParent) = TypeName(oParentCtrl)
arrCtrl(1, lngParent) = oParentCtrl.Name
Set oParentCtrl = oParentCtrl.Parent
lngParent = lngParent + 1
Loop
End If
For lngIndex = UBound(arrCtrl, 2) To 1 Step -1
Select Case arrCtrl(0, lngIndex)
Case "MultiPage"
Controls(arrCtrl(1, lngIndex)).Value = Controls(arrCtrl(1, lngIndex)).Pages(CStr(arrCtrl(1, lngIndex - 1))).Index
lngIndex = lngIndex - 1
Case "Frame"
Controls(arrCtrl(1, lngIndex)).SetFocus
End Select
Next
'The control's parent is now visible so set focus.
oCtrl.SetFocus
lbl_Exit:
Exit Sub
End Sub