Private Sub cmdSave_Click()
'CODE BUILT BY DIOGO CUBA ON 28DEC2018
'This code was built using several help requests from:
'https://access-programmers.co.uk/forums/showthread.php?p=1604347&posted=1#post1604347
'https://www.utteraccess.com/forum/index.php?showtopic=2051980&st=0&p=2706058&#entry2706058
'Special Thanks to Isladogs and Robert Crouser that helped me to understand the DCount, DLookUp and Count ("*")
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'PURPOSE:
'CHECK VALIDATION RULES ON THE FORM BEFORE SAVING THE USERNAME ON THE DATABASE. THE SELECT CASE EVALUATE THE INFORMATION SUPPLIED BY THE USER:
'>CHECK IF ALL FIELDS WERE SUPPLIED.
'>PREVENT DUPE USERNAME
'>PREVENT DUPE PEOPLE
'>CHECK THE LENGHT OF USERNAME AND PASSWORD.
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'ERROR HANDLER
'On Error GoTo ERRORHANDLER:
Select Case True
Case IsNull(Me.txtFirstName) = True Or IsNull(Me.txtMiddleName) = True Or IsNull(Me.txtLastName) = True Or IsNull(Me.cboAccessLevel) = True
Beep
Msgbox "You did not includ all information necessary to create the username." & vbCrLf & vbCrLf & "Please review all the fields and try to save again.", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtFirstName.SetFocus
Exit Sub
Case Else
'VARIABLES
Dim strFormFullName As String, strQryFullName As String, strFormUserName As String
strFormFullName = Nz(Me.txtFullName.Value, "")
strFormUserName = Nz(Me.txtUserName.Value, "")
'START ASSESSING THE FORM CONTROLS
Select Case True
'CASE 2 - CHECK IF PASSWORD IS NULL
Case IsNull(Me.txtPassword) = True
Msgbox "Password must not be empty" & vbCrLf & vbCrLf & "Please review the password and try to save again.", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtPassword.SetFocus
Exit Sub
'CASE 3 - CHECK IF PASSWORD IS SMALLER THAN 6 CHARS
Case Len(Me.txtPassword) < 6
Msgbox "Your password is too short!" & vbCrLf & vbCrLf & "Password must have at least 6 characters." & vbCrLf & vbCrLf & _
"Please review the password and try to save again.", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtPassword.SetFocus
Exit Sub
'CASE 4 - CHECK IF THE PASSWORD IS 8 CHARS OR MORE
Case Len(Me.txtPassword) > 8
Msgbox "Your password is too long!" & vbCrLf & vbCrLf & "Password must have 8 characters max." & vbCrLf & vbCrLf & _
"Please review the password and try to save again.", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtPassword.SetFocus
Exit Sub
'CASE 5 - CHECK IF THE USERNAME IS NOT EMPTY OR IS NOT SMALLER THAN 5 CHARS
Case IsNull(Me.txtUserName) = True Or Len(Me.txtUserName) < 5
Msgbox "The username is empty or it is too short!" & vbCrLf & vbCrLf & "Usernames must have have at least 5 characters." & vbCrLf & vbCrLf & _
"Please review the username and try to save again.", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtUserName.SetFocus
Exit Sub
'CASE 6 - CHECK IF THE PERSON IS ALREADY REGISTERED ON THE DATABASE
'THANKS TO ISLADOGS FOR THE CODE FIXING:
'THE DCOUNT WILL SEARCH ALL QRY AND CHECK IF THE FULLNAME IS ALREADY IN USE, IF IT IS BIGGER THAN 1 IT MEANS THERE IS ALREADY A RECORD ON THE DATABASE.
'THE QUOTATION ARE DOUBLED UP TO PICKUP NAMES WITH SPECIAL CHARS.
Case DCount("FullName", "qryCreateUsers", "FullName= """ & strFormFullName & """") > 0
Msgbox "This person already registered!" & vbCrLf & vbCrLf & "You cannot register the same person twice", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtFirstName.SetFocus
Exit Sub
'CASE 7 - CHECK IF THE USERNAME IS ALREADY SAVED ON THE DATABASE.
Case DCount("Username", "qryCreateUsers", "Username= """ & strFormUserName & """") > 0
Msgbox "This Username is already registered!" & vbCrLf & vbCrLf & "Please choose another Username and try to save again.", vbOKOnly + vbExclamation, "OPERATION FAILED"
Me.txtUserName.SetFocus
Exit Sub
'IF ALL OTHER CASES ARE OK THEN THE RECORD CAN BE SAVED.
Case Else
'TRIM AND UPPER CASE ALL FIELDS EXCEPT PASSWORD.
Me.txtFirstName = Trim(UCase(Me.txtFirstName))
Me.txtMiddleName = Trim(UCase(Me.txtMiddleName))
Me.txtLastName = Trim(UCase(Me.txtLastName))
Me.txtUserName = Trim(UCase(Me.txtUserName))
'TRIM PASSWORD
Me.txtPassword = Trim(Me.txtPassword)
Me.txtCreatedOn.Value = Now ' TIMESTAMP
Me.txtCreatedBy.Value = TempVars("Username").Value 'NAME OF THE USERNAME THAT HAS CREATED THIS USERNAME.
blnGood = True ' RESET BOOLEAN TO AUTOSAVE ON THE BOUND FORM.
Call DoCmd.RunCommand(acCmdSaveRecord) ' SAVE THE RECORD
DoCmd.GoToRecord , , acNewRec ' DISPLAY THE EMPTY RECORD FOR NEW INSERTION.
blnGood = False ' RESET BOOLEAN TO PREVENT THE AUTOSAVE ON THE BOUND FORM.
Msgbox "" & UCase(strFormUserName) & " was sucessfully created and is ready to use the application" & _
vbCrLf & vbCrLf & "Please remind him to change his password regularly", vbOKOnly + vbInformation, "SUCCESS"
Exit Sub
End Select
End Select
ERRORHANDLER:
Msgbox "An error occurred, please contact the administrator and inform the details below:" & vbCrLf & vbCrLf & _
"Error number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description & vbCrLf & vbCrLf & _
"Form: Add New User" & vbCrLf & _
"Originated on: Private Sub cmdSave_Click()", vbCritical, "Form New User Runtime Error"
Exit Sub
End Sub