vbatextboxuserformmultipage

How can I find a textbox within a multipage on my userform?


I have over a dozen textboxes requiring dates in my userform, they are split among a multipage with 5 pages.

Before the user can save updates via the userform, I loop through all controls and check the formatting on dates (among other things). If an incorrect date format is identified, the user is unable to save the updates and a message box appears which explains the error. I'd like to set the focus to the textbox in question so the user doesn't have to search through the form to find the error themselves.

The TextBox.Setfocus method does not work within a MultiPage and the page I'll need is variable depending on which textbox has been filled in incorrectly. How could I id which page the textbox sits on and set focus on it?


Solution

  • Not the most elegant solution but should serve purpose:

    Private Sub SetFocusOnPage()
        Dim i As Integer, numberOfPages As Integer
    
        numberOfPages = 5
    
        For i = 0 To numberOfPages - 1
            On Error Resume Next
                Me.MultiPage1.Value = i
                Me.CommandButton2.SetFocus
            If Err.Number = 0 Then Exit For
        Next i
    End Sub
    

    Replace MultiPage1 and CommandButton2 with your controls.