Option Explicit
'********************
' DECLARE VARIABLES..
'********************
Dim pswd1 As String 'Password 1..
Dim pswd2 As String 'Password 2..
Dim response As Integer 'Answer to msgbox (Yes/No etc..)
Dim countX As Integer 'CountX is the main number of log-ins..
Dim countZ As Integer 'CountZ is the counter for checking the "Attempt(s)" string..
Dim stringL As Integer 'String length of the Drawing Name..
Dim drawnameX As String 'Edited Drawing Name..
Dim attemptX As String 'Number of attempts the user has to log-in..
'************************
' END DECLARE VARIABLES..
'************************
'*********************
' ENTER THE PASSWORD..
'*********************
Private Sub CommandButton1_Click()
' Input text of passwords. This is simplified as there are only two users,
' but if new passwords need to be created or ammended, code to edit/add Registry
' Keys can be used. Alternatively, A small Access database can be used to store these values..
pswd1 = TextBox1.Text
pswd2 = TextBox1.Text
' Set password..
pswd1 = "bpd"
pswd2 = "roadrunner"
' Check if the textbox is empty..
If TextBox1.Text = Empty Then
MsgBox "Please enter a valid password..", vbExclamation, " Password Entry: Nil.."
Exit Sub
End If
' Test pswd entry..
If TextBox1.Text = pswd1 Or TextBox1.Text = pswd2 Then 'Password correct..
If TextBox1.Text = pswd1 Then 'If password = pswd1 then OK..
MsgBox "The password entered was correct.." & Chr(10) & " Hello Paul, " & ThisDrawing.Name & " is now open for editing..", vbExclamation, " Log-in was successful.."
ElseIf TextBox1.Text = pswd2 Then 'If password = pswd2 then OK..
MsgBox "The password entered was correct.." & Chr(10) & " Hello Mark, " & ThisDrawing.Name & " is now open for editing..", vbExclamation, " Log-in was successful.."
End If
ThisDrawing.Activate
loginform.Hide
ElseIf TextBox1.Text <> pswd1 Then 'Password incorrect..
countX = countX + 1 'Add 1 to the counter (number of log-ins)..
countZ = countZ - 1 'Remove 1 from the counter("attempts" text..
If countZ = 1 Then
attemptX = "attempt" 'If the counter = 1, then singular string..
Else: attemptX = "attempts" 'If the counter <> 1, then plural string..
End If
MsgBox "The password entered was incorrect.." & Chr(10) & "Please try again - You now have " & countZ & " " & attemptX & " left..", vbExclamation, " Password Incorrect.."
TextBox1.SetFocus
TextBox1.Text = ""
End If
' If log-in unsuccessful x3 then..
If countX = 3 Then 'If all three attempts have been used..
MsgBox "You haven't got authority to edit this drawing.." & Chr(10) & Chr(10) & "This session of AutoCAD will now close!!..", vbCritical, "Log-in Unsuccessful.."
ThisDrawing.Application.Quit
End If
'End If
End Sub
'*********************
' ENTER THE PASSWORD..
'*********************
'********************
' ENTER KEY PRESSED..
'********************
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then
CommandButton1_Click 'If user presses ENTER; this acts just as if user click CommandButton 1..
End If
End Sub
'********************
' ENTER KEY PRESSED..
'********************
'************
' FORM LOAD..
'************
Private Sub UserForm_Initialize()
' Filter out the drawing name (removes the extension)..
stringL = Len(ThisDrawing.Name) - 4
drawnameX = Mid(ThisDrawing.Name, 1, stringL)
loginform.Caption = " User Log-in: " & drawnameX & ".."
TextBox1.SetFocus 'Set focus to the textbox..
countX = 0 'Set main counter to 0..
countZ = 3 'Set text-check counter to 3..
End Sub
'************
' FORM LOAD..
'************
'**************
' FORM UNLOAD..
'**************
' If the user attempts to bypass the log-in by closing the form, the Application will close..
Private Sub UserForm_QueryClose(Cancel As Integer, closemode As Integer)
response = MsgBox("By closing this form, AutoCAD will also close.." & Chr(10) & "Are you sure you want to exit?..", vbQuestion + vbYesNo, "Close AutoCAD session..")
If response = vbNo Then
Cancel = 1
End If
If response = vbYes Then
ThisDrawing.Application.Quit 'Close the application..
End If
End Sub
'**************
' FORM UNLOAD..
'**************