TEMA 18: Programa Password
6. Programa: este programa es bastante completo. Se trata de un Formulario que nos solicita una password, se puede utilizar para programas que requieran accesos mediante clave. También incluye otro Formulario donde podemos cambiar esa clave. Además de utilizar 2 Formularios, se emplea un módulo para declarar una variable utilizada por los dos Formularios. También podemos ver funciones nuevas como Len y Chr, ver tema de Funciones y un método nuevo llamado SetFocus, ver tema de Métodos. La función MsgBox con sus valores también se emplea mucho en este programa.
| OBJETO | PROPIEDAD | VALOR |
| Form1 | BorderStyle Caption Icon MinButton Moveable |
1 Validación de usuario Monitr01.ico (Graphics, Icons, Computer) True False |
| LblPassword | Caption Font ForeColor |
Introduce la
Password: Tahoma, negrita, 12 Azul |
| Shp1 | BorderColor | Azul osucro |
| TxtPassword | Alignment Font MaxLength Text |
2-Center Tahoma, 12 8 - |
| CmdOk | Caption Font Picture Style |
Confirmar Tahoma, negrita, 10 Checkmrk.ico (Graphics, Icons, Misc) 1 |
| CmdCambiar | Caption Font Picture Style |
Cambiar Tahoma, negrita, 10 Secur05.ico (Graphics, Icons, Misc) 1 |
| CmdNuevo | Caption Font Picture Style |
Nuevo Tahoma, negrita, 10 New.bmp (Graphics, Bitmaps, Tlbr_w95) 1 |
| Form2 | BorderStyle Caption Icon MinButton Moveable |
1 Cambiar contraseña Monitr01.ico (Graphics, Icons, Computer) True False |
| LblAntigua,
LblNueva, LblConfirmar |
Caption Font |
Contraseña
antigua, Contraseña nueva, Confirmar contraseña Tahoma, 10 |
| TxtAntigua,
TxtNueva, TxtConfirmar |
Alignment Font MaxLenght Text |
2-Center Tahoma, negrita 8 - |
| CmdOk | Caption Picture Style |
- Checkmrk.ico (Graphics, Icons, Misc) 1 |
CÓDIGO DEL FORM1
Private Sub CmdCambiar_Click()
Form2.Show 1 'Cargar un segundo Formulario para cambiar contraseña
End Sub
Private Sub CmdNuevo_Click()
'Borra contraseña y pasa el foco al cuadro de texto
TxtPassword.Text = ""
TxtPassword.SetFocus
End Sub
Private Sub CmdOk_Click()
Dim Longitud As Integer
Longitud = Len(TxtPassword.Text)
'Comprueba que la longitud sea de 8 caracteres
If Longitud < 8 Then
MsgBox "LA CONTRASEÑA DEBE SER DE 8 CARACTERES", vbExclamation, _
"PASSWORD"
CmdNuevo_Click
Exit Sub
End If
'Comprueba si la contraseña es correcta o incorrecta
If TxtPassword.Text = Contrasena Then
MsgBox "LA CONTRASEÑA ES CORRECTA", vbInformation, "PASSWORD"
Else
MsgBox "LA CONTRASEÑA ES INCORRECTA", vbCritical, "PASSWORD"
End If
End Sub
Private Sub Form_Load()
Contrasena = "LUISMI10" 'Contraseña principal
End Sub
Private Sub TxtPassword_Change()
'Si contraseña es igual a 8 pasa el foco al botón de comando
Static Longitud As Integer
Longitud = Len(TxtPassword.Text)
If Longitud = 8 Then CmdOk.SetFocus
End Sub
CÓDIGO DEL FORM2
Private Sub CmdOk_Click()
Dim Longitud, Longitud2 As Integer
If TxtAntigua.Text <> Contrasena Then
MsgBox "ERROR DE CONTRASEÑA ANTIGUA", vbCritical, "CAMBIAR"
Exit Sub
End If
Longitud = Len(TxtNueva.Text): Longitud2 = Len(TxtConfirmar.Text)
If Longitud < 8 Or Longitud2 < 8 Then
MsgBox "LA CONTRASEÑA DEBE SER DE 8 CARACTERES", vbExclamation, _
"PASSWORD"
Exit Sub
End If
If TxtNueva.Text <> TxtConfirmar.Text Then
MsgBox "ERROR DE CONFIRMAR CONTRASEÑA", vbExclamation, "CAMBIAR"
Exit Sub
Else
MsgBox "LA CONTRASEÑA," + Chr(13) + "SE HA CAMBIADO CON EXITO", _
vbInformation, "CAMBIAR"
Contrasena = TxtNueva.Text
Unload Me
End If
End SubCÓDIGO DEL MÓDULO
Public Contrasena As String * 8
Bajate el programa completo haciendo clic en la imagen
![]()