Advertisement
Zidinjo

Login Access

Jun 4th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.84 KB | None | 0 0
  1. 'Main Class
  2.  
  3. Option Compare Database
  4.  
  5. Const formEmployee = "form_employee"
  6. Const menuMain = "menu_main"
  7. Const menuAdmin = "menu_admin"
  8. Const subformLoginMask = "subform_loginMask"
  9. Const subformAutostart = "subform_autostart"
  10.  
  11. Public Sub checkLoginInputs(ByVal formName As String)
  12.  
  13.     Dim userNickname As String
  14.     Dim userPassword As String
  15.     Dim userPasswordSql As String
  16.     Dim userPositionSql As String
  17.    
  18.     Dim userInputNickname As String
  19.     Dim userInputPassword As String
  20.    
  21.     'Assign nickname and password from the entered information
  22.     userInputNickname = Forms(formName).btnNickname.value
  23.     userInputPassword = Forms(formName).btnPassword.value
  24.    
  25.     'Prepare sql query
  26.     userPasswordSql = "SELECT Nickname, Password FROM tbl_employee" _
  27.     & " WHERE " & vbDoubleQuote & userInputNickname & vbDoubleQuote & "=Nickname;"
  28.    
  29.     On Error GoTo exceptionNoNicknameFound
  30.     'Get values from query
  31.     Set queryRecords = CurrentDb.OpenRecordset(userPasswordSql, dbOpenDynaset)
  32.    
  33.     'Query information assign to variable
  34.     userNickname = queryRecords(0)
  35.     userPassword = queryRecords(1)
  36.    
  37.     If (userPassword = userInputPassword) Then
  38.         'Find out what kind of position does got the employee
  39.         Dim userPositionPermissionSql As String
  40.        
  41.         userPositionPermissionSql = "SELECT Positionname FROM tbl_employee WHERE Nickname = " & vbDoubleQuote & userNickname & vbDoubleQuote
  42.         Set queryRecords = CurrentDb.OpenRecordset(userPositionPermissionSql, dbOpenDynaset)
  43.        
  44.         On Error GoTo exceptionNoPositionSelected
  45.         Call arrangeVisibility(queryRecords(0))
  46.         DoCmd.Close acForm, subformLoginMask, acSaveYes
  47.         MsgBox "You logged in as " & queryRecords(0), vbOKOnly
  48.     Else
  49.         GoTo exceptionPasswordWrong
  50.         Forms(menuMain).adminLoggedIn.value = "false"
  51.     End If
  52.    
  53. Exit Sub
  54. exceptionNoPositionSelected:
  55.     exceptionHandler ("NoPositionSelected")
  56. Exit Sub
  57. exceptionPasswordWrong:
  58.     exceptionHandler ("PasswordWrong")
  59. Exit Sub
  60. exceptionNoNicknameFound:
  61.     exceptionHandler ("NoNicknameFound")
  62.    
  63. End Sub
  64.  
  65. Private Sub setStateElement(ByVal formName As String, ByVal element As String, ByVal property As String, ByVal value As String)
  66.    
  67.     DoCmd.OpenForm formName:=formName
  68.    
  69.     Set formObj = Forms(formName)
  70.     Call propertyWrapper(formObj.Controls(element), property, value)
  71.  
  72.     DoCmd.Close acForm, formName, acSaveYes
  73.    
  74.     'Reset refs
  75.     Set formObj = Nothing
  76. End Sub
  77.  
  78. Private Function propertyWrapper(ByRef contl As Control, ByVal property As String, ByVal value As String)
  79.     'On Error GoTo exceptionPropertyDoesNotExists
  80.     Debug.Print TypeName(contl)
  81.     Select Case property
  82.         Case "value"
  83.             contl.value = value
  84.         Case "visible"
  85.             contl.visible = value
  86.         Case "Caption"
  87.             contl.Caption = value
  88.     End Select
  89.    
  90.     'propertyWrapper = contl
  91.     Exit Function
  92. exceptionPropertyDoesNotExists:
  93.     exceptionHandler ("PropertyDoesNotExists")
  94. End Function
  95.  
  96. Private Sub arrangeVisibility(ByVal position As String)
  97.     Dim userSecurityLevel As String
  98.    
  99.     'Get security level from the position
  100.     userSecurityLevel = "SELECT UserSecurityLevel FROM tbl_employeePosition WHERE PositionName = " & vbDoubleQuote & position & vbDoubleQuote & ";"
  101.     Set queryRecords = CurrentDb.OpenRecordset(userSecurityLevel, dbOpenDynaset)
  102.    
  103.     'Depend on the security level we enable or disable forms and possibilities
  104.     Select Case queryRecords(0)
  105.         Case "Admin"
  106.             Call setStateElement(menuMain, "adminLoggedIn", "Caption", "true")
  107.             Call showPanels
  108.             'Switch form to admin menu
  109.             DoCmd.OpenForm formName:=menuAdmin
  110.         Case Else
  111.             Call setStateElement(menuMain, "adminLoggedIn", "Caption", "false")
  112.             Call hidePanels
  113.             DoCmd.OpenForm formName:=menuMain
  114.     End Select
  115. End Sub
  116.  
  117. Private Sub hidePanels()
  118.     DoCmd.RunCommand acCmdWindowHide
  119.     DoCmd.ShowToolbar "Ribbon", acToolbarNo
  120.     DoCmd.SelectObject acTable, , True
  121.     DoCmd.RunCommand acCmdWindowHide
  122. End Sub
  123.  
  124. Private Sub showPanels()
  125.     'DoCmd.RunCommand acCmdWindowUnhide
  126.     DoCmd.ShowToolbar "Ribbon", acToolbarYes
  127.     DoCmd.SelectObject acForm, , True
  128. End Sub
  129.  
  130. Private Sub formVisibility(ByVal formName As String, ByVal visible As Boolean)
  131.     'Open form then set the visibility and close it otherwise a error occurs
  132.     DoCmd.OpenForm formName:=formName
  133.     Forms(formName).visible = visible
  134.     DoCmd.Close acForm, formName
  135. End Sub
  136.  
  137. 'Check if the label which is hidden in the main menu form got a value to indicate whether an admin is logged in
  138. Public Function isAdmin() As Boolean
  139.     Dim adminTextValue As String
  140.    
  141.     DoCmd.OpenForm formName:=menuMain
  142.     adminTextValue = Forms(menuMain).adminLoggedIn.Caption
  143.    
  144.     If (adminTextValue = "true") Then
  145.         DoCmd.Close acForm, menuMain
  146.         isAdmin = True
  147.     Else
  148.         isAdmin = False
  149.     End If
  150. End Function
  151.  
  152. 'Will invoke if the button access to admin area is performed
  153. Public Sub accessAdminArea()
  154.     Dim adminLoggedIn As Boolean
  155.     adminLoggedIn = isAdmin
  156.    
  157.     If (adminLoggedIn) Then
  158.         DoCmd.OpenForm formName:=menuAdmin
  159.     Else
  160.         MsgBox "You are not an admin", vbCritical, "Log in as admin"
  161.     End If
  162. End Sub
  163.  
  164. 'Will invoke if the database is opened
  165. Public Sub defaultCondition()
  166.     DoCmd.OpenForm formName:=menuMain
  167.     Forms(menuMain).adminLoggedIn.Caption = "false"
  168.     DoCmd.Close acForm, menuMain
  169.     Call hidePanels
  170. End Sub
  171.  
  172. Public Function normalizeDatabase()
  173.     Call defaultCondition
  174.     DoCmd.Close acForm, subformAutostart
  175.     DoCmd.OpenForm formName:=subformLoginMask
  176. End Function
  177.  
  178.  
  179.  
  180. 'Exceptions
  181.  
  182. Option Compare Database
  183.  
  184. 'Exceptionhandler - processes all received messages
  185. Public Sub exceptionHandler(ByVal exceptionIdentifier As String)
  186.     Select Case (exceptionIdentifier)
  187.         Case "NoEntry"
  188.             MsgBox "Add first the calculation to the database", vbCritical, "No Record found"
  189.         Case "NoForm"
  190.             MsgBox "No Form was found", vbCritical, "No Form found"
  191.         Case "NoOrderId"
  192.             MsgBox "No Order Id was found", vbCritical, "No Order Id found"
  193.         Case "SQLSyntax"
  194.             MsgBox "Sql syntax is wrong", vbCritical, "Sql syntax error"
  195.         Case "NoNicknameFound"
  196.             MsgBox "Your login name doesent exists", vbCritical, "Login name not exist"
  197.         Case "NoPositionSelected"
  198.             MsgBox "You entered employee has no position. Please inform the admin"
  199.         Case "PropertyDoesNotExists"
  200.             MsgBox "Property is not available on the transfered object", vbCritical
  201.         Case "PasswordWrong"
  202.             MsgBox "Your entered password is wrong", vbCritical, "Password invalid"
  203.     End Select
  204. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement