0

I have a button to lock unlock a form with a subform. To enable the button I would like to set a password with a msgbox. But I have an issue in my code.

I first use the code to show the password msgbox with conditional, if the password is right the user locks/unlocks the forms. But I have end with statement in wrong place.

Private Sub bloquear_Click()
    With Me.bloquear
        Dim strPasswd
        strPasswd = InputBox("Enter Password", "Restricted Form")

        If strPasswd = "" Or strPasswd = Empty Then
            MsgBox "No Input Provided", vbInformation, "Required Data"
            Exit Sub
        End If

        If strPasswd = "Password" Then
            If .Caption = "Unlock" Then
                Me.AllowAdditions = True
                Me.AllowEdits = True
                Me.CONSULTA_PRODUCTOS.Form.AllowAdditions = True
                Me.CONSULTA_PRODUCTOS.Form.AllowEdits = True
               .Caption = "Lock"
            Else
                Me.AllowAdditions = False
                Me.AllowEdits = False
                Me.CONSULTA_PRODUCTOS.Form.AllowAdditions = False
                Me.CONSULTA_PRODUCTOS.Form.AllowEdits = False
                .Caption = "Unlock"
                Me.Refresh
            End If

    End With
        Else
            MsgBox "Sorry, you do not have access to this form", _
               vbOKOnly, "Important Information"
            Exit Sub
        End If

End Sub

The msgbox shows

Compile error, end with without with

I don't know what is wrong because the with is there.

Thanks

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Ruy
  • 27
  • 4
  • 2
    Always indent/format your code nicely and correctly and you will see immediately where your issue is. Your `End With` comes before your `If` block is closed. Instead it must be in the very end right before `End Sub`. The block you open first (here `With Me.bloquear`) always needs to close last. Correct indentation makes your life **a lot** easier. It might look cumbersome/unnecessary at first but it saves you a lot of time maintaining your code and it results in less errors. • Good code in first place is a code that is easily human readable (because this results in less errors). – Pᴇʜ Oct 31 '19 at 07:16
  • Is there any way to show asterisks instead letters to hide the password in this code? – Ruy Nov 05 '19 at 11:10
  • No, not with an `InputBox` therefore you would need to create you own userform. See https://stackoverflow.com/questions/28189864/excel-vba-input-box/28195548 – Pᴇʜ Nov 05 '19 at 14:31

0 Answers0