Show Result on a label on userform

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,708
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

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
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,900
To display a result in a lable one would use syntax like
Code:
Label1.Caption = "result"
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,708
Office Version
2016
Platform
Windows
Thanks very much for pointing to the right direction. I am gratefull.
Kelly
 

Forum statistics

Threads
1,086,122
Messages
5,387,975
Members
402,092
Latest member
S_S

Some videos you may like

This Week's Hot Topics

Top