Académique Documents
Professionnel Documents
Culture Documents
If you are using the template from Online PC learning then there is no set up required.
login set up
With the module selected Right Click and choose Properties and name the userform frmLogin.
Add three labels, one for the header and one each for the text boxes and add the text you see in the
illustration below. This text will not be referred to in our code.
Name commandbutton1 cmdCheck and add a caption for this button called “Validate”
userform login
If you wish to add an image then insert and image control and locate the image. Note: make sure
your image is formatted to a small size 10 -30 kb.
These 2 userforms will be the subject of the next 2 projects. We need them now so that they can be
referenced in our code.
frmNavigation
frmSetup
It achieves 4 things:
Note: At the top of the procedure we have declared the variable [Trial] Private Trial as Long. This will
hold the number of attempts in memory. This is module level variable scope.
Copy the code below and paste it into the userform code.
Option Explicit
'Variables
user = Me.txtUser.Value
Code = Me.txtPass.Value
result = 0
'Error handler
MsgBox "Welcome Back: – " & user & " " & Code & vbCrLf _
AddData.Value = user
Current.Value = user
Unload Me
frmSetup.Show
Exit Sub
End If
If PName = CInt(Code) And PName.Offset(0, -1) = user Then ' Use this for passcode numbers only
MsgBox "Welcome Back: – " & user & " " & Code
AddData.Value = user
result = 1
Current.Value = user
Unload Me
'Show the navigation form
frmNavigation.Show
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
Exit Sub
'Error block
errHandler:
MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
End Sub
This code will stop the x button on the Userform from working.
Cancel = True
End If
End Sub
Sub Showme()
frmLogin.Show
End Sub
Sub ShowNav()
frmNavigation.Show
End Sub
ThisWorkbook Code
Sheet1.Select
ActiveWindow.DisplayWorkbookTabs = False
Showme
On Error GoTo 0
End Sub
Test the project
Note: If you want to have passcode with text and numbers combined (1234thk) instead of only text
then change this part of the macro. Text and numbers combined are treated as text.
to
Note: The ranges for the number of users can be changed to dynamic named ranges or you can
manually adjust the ranges.
Range("G2:G100")
To change this to a dynamic named range add for example a named range called “AllUsers” then
change the code to Range("AllUsers")