Zero Page with every click

0

I have in the figure above this small UserForm with three pages. On the first page, when you click the Enable button, the next date is inserted with TextBox Data as False, so the user does not change. The Enable button also enables the Predicted TextBox and the Insert and Clear buttons. When I click on the second page, I have another setting, of course, and I can enter updated data. When you click again on the first page, if you did not take any action when clicking the Enable button, you would like the page to return to its original state. I am using this code, and I would like to know if it is really this way. I'll use it for all three pages.

Private Sub MultiPage1_Click(ByVal Index As Long)
    With Me.MultiPage1
        If .Value = 0 Then Call Me.RestauraControles0
        If .Value = 1 Then Call Me.RestauraControles1
    End With
End Sub

Me.RestoreControls0 clears everything and leaves only the Enable button enabled.

    
asked by anonymous 06.08.2018 / 19:02

1 answer

0

The code works the way you are doing, but if you want something more generic create a Class Module or do it as follows:

As I do not know how the whole structure of your form is and what you really want from page 2, this is an example to learn loops in form controls and generalize some tasks that can work on every page.

Code

Option Explicit
Private Sub MultiPage1_Change()
    Dim controle As Control
    Dim pagina_atual As Long
    Dim tudo_vazio As Boolean
    tudo_vazio = True
    With Me.MultiPage1
        pagina_atual = .Value
        For Each controle In .Pages(pagina_atual).Controls
            If TypeName(controle) = "TextBox" Then
                If Trim(controle.Value) <> "" Then
                    'Debug.Print controle.Name
                    tudo_vazio = False
                    Exit For
                End If
            End If
        Next controle
        If tudo_vazio Then
            For Each controle In .Pages(pagina_atual).Controls
                If TypeName(controle) = "CommandButton" Then
                    'Debug.Print controle.Name
                    If Not Trim(controle.Name) Like "*habilitar*" Then
                        controle.Enabled = False
                    End If
                End If
            Next controle
        End If
    End With
End Sub

Explanation

When looping in the form controls of the current page and checking to see if there are no empty Text Boxes.

Current Page

The code pagina_atual = Me.MultiPage1.Value returns the value of the selected multipage page.

Loop in controls

For Each controle In Me.MultiPage1.Pages(pagina_atual).Controls
Next controle

Loops all controls on the selected multipage page.

Check if text box

If TypeName(controle) = "TextBox" Then
End If

Checks whether the control is a text box.

Check if something was written in the Text Box

If Trim(controle.Value) <> "" Then
End If

Checks if the value of the text box is different from empty.

If it is not empty

tudo_vazio = False
Exit For

If the control is not empty, that is, with some value. Changes the flag tudo_vazio to False and exits the loop.

Check the flag tudo_vazio

If tudo_vazio Then
End If

If tudo_vazio is true, continue with the code.

Loop controls and check button

As before, the loop in the controls is checked and checked to see if it is a Command Button.

Check Command Button Name

As the Text_Box are all empty, it means that there was no change. Then all buttons that do not contain the word enable in the name will be disabled with controle.Enabled = False

If Not Trim(controle.Name) Like "*habilitar*" Then
End If
    
06.08.2018 / 21:02