You are on page 1of 8

Hoja1(Principal o INICIO)

Private Sub btn_Admin_Click()


frm_Admin.Show
End Sub

CommandButton1_Click Botn Cambiar de Usuario


Private Sub CommandButton1_Click()
Call OcultarHojas
frm_Login.Show
End Sub

Mdulo1
Sub MostrarHojas()
Dim Hoja As Worksheet
For Each Hoja In Worksheets
If Hoja.CodeName <> "Hoja1" Then
Hoja.Visible = xlSheetVisible
End If
Next Hoja
End Sub
Sub OcultarHojas()
Dim Hoja As Worksheet
For Each Hoja In Worksheets
If Hoja.CodeName <> "Hoja1" Then
Hoja.Visible = xlSheetVeryHidden
End If
Next Hoja
End Sub
Sub Menu_Principal()
frm_Menu.Show
End Sub

ThisWorkBook
Private Sub Workbook_Open()
frm_Login.Show
End Sub

Private Sub
CommandButton1_Click()
Call MostrarHojas
End Sub
Private Sub CommandButton2_Click()
Call OcultarHojas
End Sub
Private Sub CommandButton3_Click()
frm_NuevoUsuario.Show
End Sub
Private Sub CommandButton4_Click()
frm_EliminarUsuario.Show
End Sub

CommandButton1 (Botn Eliminar Usuario)


Private Sub CommandButton1_Click()
Dim Fila As Integer

Dim final As Integer


For Fila = 2 To 1000
If Hoja6.Cells(Fila, 1) = "" Then
final = Fila - 1
Exit For
End If
Next
If MsgBox("Seguro que quiere eliminar este Usuario?", vbQuestion + vbYesNo) = vbYes
Then
For Fila = 2 To final
If Me.ComboBox1 = Hoja6.Cells(Fila, 1) Then
Hoja6.Cells(Fila, 1).EntireRow.Delete
Exit For
End If
Next
MsgBox "Usuario eliminado", vbInformation + vbOKOnly
Else
Exit Sub
End If
Me.ComboBox1 = ""
Me.txt_Status = ""
End Sub

Botn Finalizar o Salir


Private Sub CommandButton2_Click()
Unload Me
End Sub

CommandButton1_Click (Registrar Nuevo Usuario)


Private Sub CommandButton1_Click()
Dim Fila As Integer
Dim final As Integer
Dim Registro As Integer

For Fila = 1 To 1000


If Hoja6.Cells(Fila, 1) = "" Then
final = Fila
Exit For
End If
Next

For Registro = 2 To final


If Hoja6.Cells(Registro, 1) = Me.txt_nUser Then
Me.txt_nUser.BackColor = &H8080FF
MsgBox ("El usuario ya existe" + Chr(13) + "Ingrese un usuario diferente")
Me.txt_nUser.SetFocus
Exit Sub
Exit For
End If
Next
If Me.txt_pass1 = Me.txt_pass2 Then
Me.txt_nUser.BackColor = &HFFFFFF
Hoja6.Cells(final, 1) = Me.txt_nUser
Hoja6.Cells(final, 2) = Me.txt_pass1
If Me.OptionButton1.Value = True Then
Hoja6.Cells(final, 3) = "empleado"
Else
Hoja6.Cells(final, 3) = "Admin"
End If
'-----------------------------------------------

Me.txt_nUser = ""
Me.txt_pass1 = ""
Me.txt_pass2 = ""
Me.txt_nUser.SetFocus
Else
MsgBox "Las contraseas deben coincidir"
End If
End Sub

CommandButton2_Click Botn (Finalizar)


Private Sub CommandButton2_Click()
Unload Me
End Sub

btn_Registrar
Private Sub btn_Registrar_Click()
Dim usuario As String
Dim Fila, final As Integer
Dim password, UsuarioEncontrado, yaExiste, Status
Dim Rango As Range
Titulo = "Gestor de Inventarios"

yaExiste = Application.WorksheetFunction.CountIf(Hoja6.Range("Tabla1[Usuario]"),
Me.txtUsuario.Value)
Set Rango = Hoja6.Range("Tabla1[Usuario]")

If Me.txtUsuario.Value = "" Or Me.txtPassword.Value = "" Then


MsgBox "Introduce usuario y contrasea", vbExclamation, Titulo
Me.txtUsuario.SetFocus
ElseIf yaExiste = 0 Then
MsgBox "El usuario '" & Me.txtUsuario & "' no existe", vbExclamation, Titulo
ElseIf yaExiste = 1 Then
UsuarioEncontrado = Rango.Find(What:=Me.txtUsuario.Value,
MatchCase:=False).Address
password = Hoja6.Range(UsuarioEncontrado).Offset(0, 1).Value
Status = Hoja6.Range(UsuarioEncontrado).Offset(0, 2).Value

If Hoja6.Range(UsuarioEncontrado).Value = Me.txtUsuario.Value And password =


Me.txtPassword.Value Then
For Fila = 1 To 1000
If Hoja8.Cells(Fila, 1) = "" Then
final = Fila
Exit For
End If
Next
Hoja8.Cells(final, 1) = "=NOW()"
Hoja8.Cells(final, 1).Copy
Hoja8.Cells(final, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Hoja8.Cells(final, 2) = Me.txtUsuario
Hoja8.Cells(final, 3) = Status
If Status = "Admin" Then
Hoja1.btn_Admin.Enabled = True
End If
Hoja8.Range("G1") = Me.txtUsuario

Unload Me
Else
MsgBox "La contrasea es incorrecta", vbExclamation, Titulo
End If
End If
End Sub

btn_Salir
Private Sub btn_Salir_Click()
Unload Me
ActiveWorkbook.Save
Application.DisplayAlerts = False
Application.Quit
End Sub

UserForm_Initialize
Private Sub UserForm_Initialize()
Call OcultarHojas
Hoja1.btn_Admin.Enabled = False
End Sub

UserForm_QueryClose
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Unload Me
ActiveWorkbook.Save
Application.DisplayAlerts = False
Application.Quit
'Cancel = 1
'CloseMode = 1
End If
End Sub