Re: Excel 2007 - LogIn Logout
Hi here i post my simple code as requested.
=====CODE FOR LOG IN=====
Code
Option Explicit
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOK_Click()
Dim iFoundPass As Integer
'Dim LogIN
'Dim today
On Error Resume Next
With Sheets("UserAccount").Range("UserNames")
iFoundPass = .Find(What:=txtusername, After:=.Cells(1, 1), LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Row
End With
On Error GoTo 0
If iFoundPass = 0 Then
SomethingWrong
Exit Sub
End If
If Sheets("UserAccount").Cells(iFoundPass, 2) <> txtpass Then
SomethingWrong
Exit Sub
Else
MsgBox "LogIn success", vbInformation, "LogIn Successful"
'Write in the cell to view the log in info
Dim lrow As Long
Dim ws As Worksheet
Set ws = Worksheets("DTR")
'find empty cells to fill in with appropriate information to record/ no password will be recorded
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'copy data to DTR
ws.Cells(lrow, 1).Value = Sheets("UserAccount").Cells(iFoundPass, 3)
ws.Cells(lrow, 2).Value = Me.txtusername.Value
ws.Cells(lrow, 3).Value = Now()
'clear the textboxes
txtusername.Value = ""
txtpass.Value = ""
End If
'Write to a cell
' LIN = Now()
'Sheets("DTR").Range("fullname") = LogIN
'Exit the form
'Unload Me
End Sub
Private Sub txtpass_Change()
cmdOK.Enabled = (txtusername.TextLength > 2 And _
txtpass.TextLength > 2)
End Sub
Private Sub fLogIN_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then Cancel = True
End Sub
Private Sub txtusername_Change()
cmdOK.Enabled = (txtusername.TextLength > 2 And _
txtpass.TextLength > 2)
End Sub
Private Sub SomethingWrong()
MsgBox "Incorrect Username or Password.", vbCritical + vbInformation, "LogIn Error"
txtusername.SetFocus
End Sub
Private Sub UserForm_Click()
End Sub
Display More
======CODE FOR LOGOUT=====
Code
Private Sub cmdexit_Click()
Unload Me
End Sub
Private Sub cmdLogOUT_Click()
Dim iFoundPass As Integer
'Dim LogIN
'Dim today
On Error Resume Next
With Sheets("UserAccount").Range("UserNames")
iFoundPass = .Find(What:=txtusername, After:=.Cells(1, 1), LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Row
End With
On Error GoTo 0
If iFoundPass = 0 Then
SomethingWrong
Exit Sub
End If
If Sheets("UserAccount").Cells(iFoundPass, 2) <> txtpass Then
SomethingWrong
Exit Sub
Else
MsgBox "Log Out success", vbInformation, "Log Out Successful"
'Write in the cell to view the log out info
Dim lrow As Long
Dim ws As Worksheet
Set ws = Worksheets("DTR")
'find empty cells to fill in with appropriate information to record/ no password will be recorded
lrow = ws.Cells(Rows.Count, 4).End(xlUp).Offset(1, 4).Row
'copy data to DTR
'ws.Cells(lrow, 1).Value = Sheets("UserAccount").Cells(iFoundPass, 3)
'ws.Cells(lrow, 2).Value = Me.txtusername.Value
'ws.Cells(lrow, 3).Value = Now()
'write the log out time iin 4th ROW
ws.Cells(lrow, 4).Value = Now()
'clear the textboxes
txtusername.Value = ""
txtpass.Value = ""
End If
'Write to a cell
' LIN = Now()
'Sheets("DTR").Range("fullname") = LogIN
'Exit the form
'Unload Me
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub txtpass_Change()
cmdLogOUT.Enabled = (txtusername.TextLength > 2 And _
txtpass.TextLength > 2)
End Sub
Private Sub txtusername_Change()
cmdLogOUT.Enabled = (txtusername.TextLength > 2 And _
txtpass.TextLength > 2)
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
End Sub
Private Sub SomethingWrong()
MsgBox "Incorrect Username or Password.", vbCritical + vbInformation, "LogIn Error"
txtusername.SetFocus
End Sub
Display More