i need to enhance the existing code to add on the user locked if the user failed 3 times... even after they enter the username & password, the system should track whether the user have been tried to login before... once it have been try for three times, the user should be locked by system , administrator should unlock the user account.. please help me... thanks...

Are you using any database? Are you keeping the details of user name and password in DB?

you need to store user details in DB.

1. check the status of the use account with each login.
2. increment the counter by 1 with each failed login.
3. update the status of the user to locked once the counter reaches 3.

commented: Thanks a lot...its really helpful.. +0

yes im using sql database to store the user name and password.. i have done the increment by 1 and after it reach 3 (the message box will appear said "Please contact administrator") ... after the 3 times, the user still can access into the system.... how should I prevent the user from login into the system after 3 times...

read point #1 of my previous post.

Yes as debasis said, Have one column like Status or somthing, Update it to Incactive if attemt reaches to 3+ and check this status while ur doing validation when user hits on logon button. If Status is Inactive give message and exit..

i think you are only displaying the message, but actually not locking the user on 3 failed login attempts.

Module .bas

Public path As String


Sub Main()


Set db = New ADODB.Connection

path = App.path & "\Data\yourdb.mdb"
db.CursorLocation = adUseClient
db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & path

frmODBCLogon.Show
End Sub

frmODBCLogon (Form)

Private Sub CmdOk_Click()
Set rs = New ADODB.Recordset
Static countx
Static CountY
CountY = 1
If txtUserName.Text <> "" And txtPassword.Text <> "" Then
    rs.Open "Select * from Users where Username ='" & CStr(txtUserName.Text) & "' and Password ='" & CStr(txtPassword.Text) & "'", db, 2, 3
        If rs.EOF Then
            MsgBox "User authentication Failed!Invalid username or password! Please Try Again!", vbExclamation, "INVALID ACCOUNT"
            countx = countx + CountY
                If countx = 3 Then
                    MsgBox "Please contact administrator!", vbCritical, "ERROR"
                    
                    'Locked the user's status account
                    Dim stat As String
            
                    stat = "locked"
                    
                     db.Execute ("Update [Users] set [Status]='" & stat & "' WHERE [Username]='" & txtUserName.Text & "'")
                End If
                
            countx = countx
            txtUserName.Text = ""
            txtPassword.Text = ""
            txtUserName.SetFocus
        Else
           
            Unload Me
            MsgBox "Access Granted!!"
            
            With frmMain
             
                .Show
            End With
        End If
    rs.Close
Else
    MsgBox "Cannot Login! Insert Username/Password!", vbExclamation, "ERROR"
    txtUserName.SetFocus
End If
End Sub

All you really need is a few conditionals with your events in between as such:

dim attempts as integer

attempts = 0

private sub Login()
if attempts <= 3 then
		if username = "username" and password = "password" then
			'login
		else
			attempts = attempts + 1
		end if
else
'contact systems admin
end if

End sub



private sub Reset()
	If adminUser = "adminUser" and adminPass = "adminPass" then
		attempts = 0
	end if
End sub

PM me if this is helpful so i know im posting working stuff and not complete gibberish

Hi Guys, I really need a help on this issue.. I have the same problem here..

i need to enhance the existing code to add on the user locked if the user failed 3 times... even after they enter the username & password, the system should track whether the user have been tried to login before... once it have been try for three times, the user should be locked by system , administrator should unlock the user account.. please help me... thanks...

Currently, below are my coding.... please help me to find out where did i go wrong... Thanks so much.....

Option Explicit

Dim g_objRSx As ADODB.Recordset

Private Sub cmdCancel_Click()
'set the global var to false
'to denote a failed login
LoginSucceeded = False
loginUserType = ""
Me.Hide
End Sub
Private Sub cmdOK_Click()

'check for correct password
loginfailed_cnt = loginfailed_cnt + 1

If chkpassword = True Then
'setting a global var is the easiest
LoginSucceeded = True
'update the tblog table for good login
Loginstatus ("Successful")
Me.Hide
Else
If loginfailed_cnt = 3 Then
Loginstatus ("3 invalid logins")
MsgBox "You have made 3 invalid attempts. Bye..."
Unload frmLogin
Unload frmmainmenu1
Exit Sub
End If
txtPassword.SetFocus
SendKeys "{Home}+{End}"
End If
End Sub
Function chkpassword() As Boolean

Dim sqlline As String

Set g_objRSx = New ADODB.Recordset

sqlline = "SELECT USERTYPE, USERNAME " _
          + "FROM TBUSER " _
          + "WHERE [USERID] = " & cnvuserid() & " AND" _
          + " [USERPWD] = " & cnvuserpwd & ""

g_objRSx.Open sqlline, g_objConn, adOpenStatic, adLockReadOnly

If g_objRSx.RecordCount > 0 Then

    'chkpassword = True
    g_objRSx.MoveFirst
    Do Until g_objRSx.EOF
     'Assign the access level in the public variable
        loginUserType = Trim(UCase(g_objRSx!UserType.Value))
        g_objRSx.MoveNext
    Loop
  On Error GoTo 0

Else
MsgBox "login failed, pls try again OR contact your Administrator"

  g_objRSx.Close
  loginUserType = ""
  chkpassword = False

  Exit Function

End If

g_objRSx.Close

chkpassword = True

End Function
Function cnvuserid() As String
Dim xstr As String
xstr = "'" & UCase(Trim(txtUserName.Text)) & "'"
cnvuserid = xstr
End Function
Function cnvuserpwd() As String
Dim xxstr As String
xxstr = "'" & UCase(Trim(txtPassword.Text)) & "'"
cnvuserpwd = xxstr
End Function
Function cnvlogdate() As String
'' to get the current date
'cnvlogdate = "CDATE(" & "'" & Trim(Format(Date, "dd-mm-yyyy")) & "'" & ")"
'cnvlogdate = "CDATE(" & "'" & Trim(FormatDateTime(Date, vbGeneralDate)) & "'" & ")"
cnvlogdate = Trim(FormatDateTime(Date, vbGeneralDate))
'FormatDateTime(Date[,NamedFormat])
End Function

Function cnvXwhat(Xwhat) As String
'' to get the current date
cnvXwhat = "'" & UCase(Trim(Xwhat)) & "'"
End Function

Function Loginstatus(Xwhat As String)
Dim mStr As String
Dim Sqlline1 As String
Dim mydate, mytime

mydate = Format(Now, "dd-mmm-yyyy")
mytime = Time

mStr = Trim(UCase(Xwhat))

Set g_objRSx = New ADODB.Recordset

g_objConn.Execute " INSERT INTO TBLOG " _
                & "(DTLOGDATE, DTLOGTIME, SUSERID, SLOGDESC) " _
                & "VALUES ('" & mydate & "','" & mytime & "'," & cnvuserid() & "," _
                & cnvXwhat(Xwhat) & ");"

End Function

hi guys
How to validate a username and password to login and then also check the usertype, based on which different events can be triggered?

for example admin has certain privileges and normal user has different privileges.
my code:
Public Class Login

Dim con As New OleDb.OleDbConnection
'new connection to database
Dim dbprovider As String
'to gets the probider name
Dim dbsource As String
'to gets the database provider name
Dim ds As New DataSet
'dataset to table
Dim da As OleDb.OleDbDataAdapter
'databaseAdapter to dataset and database
Dim sql As String
'sql command
Dim usrname1, pswd1, usrtype As String
Dim maxrows, incdec As Integer
'string variables

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'validation of username and password
    If txtb_uname.Text = usrname1 And txtb_pwd.Text = pswd1 Then
            If usrtype = "admin" Then

                Score.Show()
                Score.btn_delete.Enabled = False
                Score.btn_update.Enabled = False
                Score.Button2.Enabled = False
                Score.Button1.Enabled = False


            Else
                MsgBox("invalid usertype")
            End If
            'username and password correct go to the netx page
    ElseIf txtb_uname.Text = usrname1 And txtb_pwd.Text = "" Then
            MsgBox("Enter Password")
            'blank password control
    ElseIf txtb_uname.Text = "" And txtb_pwd.Text = pswd1 Then
            MsgBox("Enter Username")
            'blank username control
    ElseIf txtb_uname.Text = usrname1 And txtb_pwd.Text <> pswd1 Then
            MsgBox("Invalid Password")
            'incorrect pasword
    ElseIf txtb_uname.Text <> usrname1 And txtb_pwd.Text = pswd1 Then
            MsgBox("Invalid Username")
            'incorrect username
    ElseIf txtb_uname.Text = "" And txtb_pwd.Text = "" Then
            MsgBox("enter Username")
            'blank username and password

    Else
            MsgBox("Invalid Username & or Password")
            'incorrect username and password

    End If


End Sub

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    dbprovider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
    'database provider name
    dbsource = "Data Source = C:\Users\s562\Desktop\DB1.accdb"
    'file name of the database source
    con.ConnectionString = dbprovider & dbsource
    'to access database from form
    con.Open()
    'MsgBox("DAtabase open")
    sql = "select * from login"
    'select all records

    da = New OleDb.OleDbDataAdapter(sql, con)
    'execute sql command
    da.Fill(ds, "login1")
    maxrows = ds.Tables("login1").Rows.Count
    'count table rows
    ' incdec = 0
    'incdec = incdec + 1
        pswd1 = ds.Tables("login1").Rows(0).Item(2)
        'table value stored in pswd1
        usrname1 = ds.Tables("login1").Rows(0).Item(1)
        'table value stored in usrname1
        usrtype = ds.Tables("login1").Rows(0).Item(0)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

End Sub

End Class

please give me answers

Help i need to have 3 login attempts then the program will lock down but the message box shows up 3 times consecutively, it will close then pops out it will do this for 3 times then it closes ... help pleassseee :(

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    Dim un, pw As String
    Dim counter As Integer = 0
    un = txtUsername.Text
    pw = txtPassword.Text

    Do 
        If un = "student" And pw = "21217" Then
            Me.Hide()
            CourseStudy.Show()
        Else
            counter = counter + 1
            MsgBox("Try again", MsgBoxStyle.Critical, "Acess Denied")
            txtPassword.Text = ""
            txtUsername.Text = ""

        End If

    Loop While counter <= 2

    Me.Close()
    Form1.Close()
    MessageBox.Show("Program exited, Try again", "Acess Denied")

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.