Rapport de message :*
 

Re: Mot de passe sur bouton

Titre du sujet : Re: Mot de passe sur bouton
par david84 le 15/05/2013 19:46:15

Bonsoir Débutant, Jean-Claude, papyjac, le forum,

ci-joint une possibilité à tester et à affiner selon ton besoin (désolé mais je ne trouve pas comment insérer le code dans les balises adéquates) :

Code à placer dans un module :

Option Explicit
Sub Creer_usftemp() 'macro adaptée d'un exemple trouvé sur http://silkyroad.developpez.com/VBA/VisualBasicEditor/
Dim UsfForm As Object
Dim NewB As MSForms.CommandButton
Dim TxtB As MSForms.TextBox
Dim i As Long
Dim UsfName As String

If MsgBox("Etes-vous le concepteur du programme ?", 4 + 32, "Demande du concepteur") = vbYes Then

    Application.VBE.MainWindow.Visible = False
    Set UsfForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    
    With UsfForm
        .Properties("Caption") = "Entrez votre mot de passe"
        .Properties("Width") = 180
        .Properties("Height") = 80
        UsfName = UsfForm.Name
    End With

    Set TxtB = UsfForm.Designer.Controls.Add("Forms.Textbox.1", , True)
    With TxtB
        .Top = 20
        .Height = 20
        .Left = 10
        .Width = 70
        .BorderStyle = fmBorderStyleSingle
        .PasswordChar = "*"
    End With
    
    Set NewB = UsfForm.Designer.Controls.Add("Forms.CommandButton.1")
    
    With NewB
        .Height = 20
        .Width = 70
        .Left = 180 - 80
        .Top = 20
        .Caption = "Valider"
        .Name = "cmdValider"
    End With
    
    With UsfForm.CodeModule
        i = .CountOfLines
        .InsertLines i, "Private Sub cmdValider_Click()": i = i + 1
        .InsertLines i, "Dim mdp As String": i = i + 1
        .InsertLines i, "mdp = ""toto""": i = i + 1
        .InsertLines i, "If Me.TextBox1.Text <> mdp Then": i = i + 1
        .InsertLines i, "MsgBox ""mot de passe incorrect"": ActiveWindow.DisplayWorkbookTabs = False": i = i + 1
        .InsertLines i, "Else": i = i + 1
        .InsertLines i, "ActiveWindow.DisplayWorkbookTabs = True": i = i + 1
        .InsertLines i, "End If": i = i + 1
        .InsertLines i, "UnLoad Me": i = i + 1
        .InsertLines i, "End Sub"
    End With
 
    VBA.UserForms.Add(UsfName).Show
    ThisWorkbook.VBProject.VBComponents.Remove VBComponent:=UsfForm
Else
    ActiveWindow.DisplayWorkbookTabs = False
End If
End Sub

 

Et dans le code du bouton de commande :

Private Sub CommandButton1_Click()
Call Creer_usftemp
End Sub

 

A+