kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- Windows
Hello everyone;
I have this code from the internet for my login form. I have done some modification to it. Now I wanna do something cooler: I wanna have the attempts left displayed in a label instead of the textbox I have used. I named the textbox Reg1 as in the code below. I believe there is a cooler way. But I cant figure it out yet. Thanks
Kelly
I have this code from the internet for my login form. I have done some modification to it. Now I wanna do something cooler: I wanna have the attempts left displayed in a label instead of the textbox I have used. I named the textbox Reg1 as in the code below. I believe there is a cooler way. But I cant figure it out yet. Thanks
Kelly
Code:
Option Explicit
Private Trial As Long
Private Sub UserForm_Initialize()
Reg1.Value = "You have 3 attempts left"
Reg1.Enabled = False
End Sub
Private Sub cmdCheck_Click()
Dim AddData As Range
Dim user As Variant, Code As Variant, result As Integer, TitleStr As String, _
Current As Range, PName As Variant, UName As Variant, msg As VbMsgBoxResult
user = Me.txtUser.Value
Code = Me.txtPass.Value
TitleStr = "Password check"
result = 0
Set Current = Sheet13.Range("K2")
On Error GoTo errHandler:
Set AddData = Sheet13.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)
If user = "SDLS" And Code = "1234M" Then
MsgBox "Welcome Back: - " & user, vbInformation
AddData.Value = user
AddData.Offset(0, 1).Value = Now
Current.Value = user
Unload Me
Exit Sub
End If
For Each UName In Sheet13.Range("G2:G110")
If user <> "" And Code <> "" And UName = user Then
For Each PName In Sheet13.Range("H2:H110")
If PName = Code Then 'Use this for passcode text
'If PName = CInt(Code) And PName.Offset(0, -1) = user Then ' Use this for passcode numbers only
MsgBox "Welcome Back: - " & user
AddData.Value = user
AddData.Offset(0, 1).Value = Now
result = 1
Current.Value = user
Unload Me
Exit Sub
End If
Next PName
End If
Next UName
If result = 0 Then
Trial = Trial + 1
Reg1.Value = "You have" & " " & 3 - Trial & " " & "attempts left"
If Trial < 3 Then msg = MsgBox("Wrong password, please try again", vbExclamation + vbOKOnly, TitleStr)
Me.txtUser.SetFocus
If Trial = 2 Then
Reg1.Value = "You have" & " " & 3 - Trial & " " & "attempt left"
End If
If Trial = 3 Then
msg = MsgBox("All attempts failed, the form will close…", vbCritical + vbOKOnly, TitleStr)
ActiveWorkbook.Close False
End If
End If
Exit Sub
errHandler:
MsgBox "An Error has Occurred "
End Sub