greenhillchris
New Member
- Joined
- Mar 5, 2022
- Messages
- 18
- Office Version
- 365
Hi, I have a workbook with different sheets and only want certain users to see ceratin sheets. I have usernames and passwords on a login sheet within this book. I have got the following code entered in the form however evertime I run it and enter the usernames and passwords they are not recgoinsed and cant login. Not sure where I am going wrong! Any help on why the code is working would be a great help.
[
Option Explicit
Private Trial As Long
Private Sub cmdCheck_Click()
Dim AddData As Range, Current As Range
Dim user As Variant, Code As Variant
Dim PName As Variant, AName As Variant
Dim ws As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim result As Integer
Dim TitleStr As String
Dim msg As VbMsgBoxResult
user = Me.txtUser.Value
Code = Me.txtPass.Value
TitleStr = "Password check"
result = 0
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each AName In Sheet9.Range("J2:J10")
If AName = Code Then
Sheet9.Visible = True
Sheet9.Select
Unload Me
Exit Sub
End If
Next AName
End If
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each PName In Sheet9.Range("b2:b108")
If PName = Code Then
If PName.Offset(0, 1) <> "" Then
Set ws = Worksheets(PName.Offset(0, 1).Value)
ws.Visible = True
End If
If PName.Offset(0, 2) <> "" Then
Set ws2 = Worksheets(PName.Offset(0, 2).Value)
ws2.Visible = True
End If
If PName.Offset(0, 3) <> "" Then
Set ws3 = Worksheets(PName.Offset(0, 3).Value)
ws3.Visible = True
End If
ActiveWindow.DisplayWorkbookTabs = True
result = 1
Unload Me
End
Exit Sub
End If
Next PName
End If
If result = 0 Then
Trial = Trial + 1
If Trial < 3 Then msg = MsgBox("Wrong password, please try again", vbExclamation + vbOKOnly, TitleStr)
Me.txtUser.SetFocus
If Trial = 3 Then
msg = MsgBox("Wrong password, the form will close…", vbCritical + vbOKOnly, TitleStr)
ActiveWorkbook.Close False
End If
End If
End Sub
]
[
Option Explicit
Private Trial As Long
Private Sub cmdCheck_Click()
Dim AddData As Range, Current As Range
Dim user As Variant, Code As Variant
Dim PName As Variant, AName As Variant
Dim ws As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim result As Integer
Dim TitleStr As String
Dim msg As VbMsgBoxResult
user = Me.txtUser.Value
Code = Me.txtPass.Value
TitleStr = "Password check"
result = 0
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each AName In Sheet9.Range("J2:J10")
If AName = Code Then
Sheet9.Visible = True
Sheet9.Select
Unload Me
Exit Sub
End If
Next AName
End If
If user <> "" And Not IsNumeric(user) And Code <> "" And IsNumeric(Code) Then
For Each PName In Sheet9.Range("b2:b108")
If PName = Code Then
If PName.Offset(0, 1) <> "" Then
Set ws = Worksheets(PName.Offset(0, 1).Value)
ws.Visible = True
End If
If PName.Offset(0, 2) <> "" Then
Set ws2 = Worksheets(PName.Offset(0, 2).Value)
ws2.Visible = True
End If
If PName.Offset(0, 3) <> "" Then
Set ws3 = Worksheets(PName.Offset(0, 3).Value)
ws3.Visible = True
End If
ActiveWindow.DisplayWorkbookTabs = True
result = 1
Unload Me
End
Exit Sub
End If
Next PName
End If
If result = 0 Then
Trial = Trial + 1
If Trial < 3 Then msg = MsgBox("Wrong password, please try again", vbExclamation + vbOKOnly, TitleStr)
Me.txtUser.SetFocus
If Trial = 3 Then
msg = MsgBox("Wrong password, the form will close…", vbCritical + vbOKOnly, TitleStr)
ActiveWorkbook.Close False
End If
End If
End Sub
]