vb6 Login form code error - authentication

I set up a database with one entry for my login form and it worked well. After populating the same database, I can't login using the data added to the database. I'm stuck with the first entry from my table.
here is the code:
Set recset = New ADODB.Recordset
sql = "select * from tblLogin"
recset.Open sql, connect, adOpenDynamic, adLockOptimistic
recset.MoveFirst
Do While Not recset.EOF
If recset("Username").Value = txtUsername.Text Or txtUsername.Text = "harenama" Then
usname = True
If recset("Password").Value = txtPassword.Text Or txtPassword.Text = "sankirtan" Then
uspass = True
If recset("Usertype").Value = "user" Then
main.mnuAddUser.Enabled = False
End If
txtPassword.Text = ""
txtUsername.Text = ""
main.Show
Me.Hide
Else
uspass = False
MsgBox "Invalid Login! Incorrect Password", vbOKOnly, "Login"
txtPassword.Text = ""
txtUsername.Text = ""
txtUsername.SetFocus
Exit Do
Exit Sub
End If
Exit Sub
Else
usname = False
MsgBox "Invalid Login! Username not found.", vbOKOnly, "Login"
txtPassword.Text = ""
txtUsername.Text = ""
Exit Do
Exit Sub
End If
recset.MoveNext
Loop
recset.Close
connect.Close

In your code, you're looking at the first record of the recordset. Keep that in mind as you go through this Method.
IF
The username matches what the user typed in, we will look at the
passwords.
If the passwords match, we will look at the UserType.
Then, we show main, whatever that is.
THEN you will exit the Sub.
ELSE
If the username doesn't match anything in the first record, the fields are cleared and we are exiting the Do Loop and the Sub.
What I would expect to see : If the usernames don't match, move on to the next record. Only at recset.EOF would I expect to be kicked out of the Sub - because only then can you say you have analyzed every record.
Set recset = New ADODB.Recordset
sql = "select * from tblLogin"
recset.Open sql, connect, adOpenDynamic, adLockOptimistic
recset.MoveFirst
Do While Not recset.EOF
If recset("Username").Value = txtUsername.Text Or txtUsername.Text = "harenama" Then
usname = True
If recset("Password").Value = txtPassword.Text Or txtPassword.Text = "sankirtan" Then
uspass = True
If recset("Usertype").Value = "user" Then
main.mnuAddUser.Enabled = False
End If
txtPassword.Text = ""
txtUsername.Text = ""
main.Show
Me.Hide
Else
uspass = False
MsgBox "Invalid Login! Incorrect Password", vbOKOnly, "Login"
txtPassword.Text = ""
txtUsername.Text = ""
txtUsername.SetFocus
Exit Do
Exit Sub
End If
Exit Sub
Else
usname = False
MsgBox "Invalid Login! Username not found.", vbOKOnly, "Login"
txtPassword.Text = ""
txtUsername.Text = ""
Exit Do ' These are ending your search
Exit Sub
End If
recset.MoveNext ' So you're never getting here
Loop
recset.Close
connect.Close

Here is my solution
Set recset = New ADODB.Recordset
sql = "select * from tblLogin"
recset.Open sql, connect, adOpenDynamic, adLockOptimistic
recset.MoveFirst
Do While Not recset.EOF
If recset("Username").Value = txtUsername.Text Or txtUsername.Text = "harenama" Then
usname = True
If recset("Password").Value = txtPassword.Text Or txtPassword.Text = "sankirtan" Then
uspass = True
If recset("Usertype").Value = "user" Then
main.mnuAddUser.Enabled = False
End If
txtPassword.Text = ""
txtUsername.Text = ""
main.Show
Me.Hide
Else
uspass = False
MsgBox "Invalid Login! Incorrect Password", vbOKOnly, "Login"
txtPassword.Text = ""
txtUsername.Text = ""
txtUsername.SetFocus
End If
End If
recset.MoveNext
Loop
'I placed this outside the loop as it will execute wether the conditions are met or not
If usname = False Then
MsgBox "Invalid Login! Username not found.", vbOKOnly, "Login"
txtPassword.Text = ""
txtUsername.Text = ""
End If
recset.Close
connect.Close`
The Method goes like this
If username matches with user input then, it will check the password
Getting the correct password, it will move to check the usertype
If password is wrong, a prompt message will come out.
If username does not match recset.Movenext will check the next line if the username matches doing the loop until recset.EOF.

Related

How Can Fix "run time error 3251" in excel VBA

I am using excel 2016., I used a form to fill data in access, but when click refresh I start getting "run time error 3251" on this one. If can someone help me to find out. please.
I am fetching data from access to excel VBA user form & then doing some updates & deleting as per requirement.
Error
Msg=> "Run-time error '3251': Current Recordset does not support
updating. This may be a limitation of the provider, or of the
selected locktype."
Code
Private Sub CommandButton1_Click()
''''''''Add Validation here '''''''''''''
If IsDate(Me.txtdate1.Value) = False Then
MsgBox "Please enter the correct Transaction_Date", vbCritical
Exit Sub
End If
If Me.txtcampany1.Value = "" Then
MsgBox "Please enter the Campany", vbCritical
Exit Sub
End If
If Me.txttrans1.Value = "" Then
MsgBox "Please enter the Type_Transaction", vbCritical
Exit Sub
End If
If Me.txtdebit.Value <> "" Then
If IsNumeric(Me.txtdebit.Value) = False Then
MsgBox "Please enter the correct Debit", vbCritical
Exit Sub
End If
End If
If Me.txtcredit.Value <> "" Then
If IsNumeric(Me.txtcredit.Value) = False Then
MsgBox "Please enter the correct credit", vbCritical
Exit Sub
End If
End If
If Me.txtbank1.Value = "" Then
MsgBox "Please enter the By_Bank", vbCritical
Exit Sub
End If
If Me.txtStuff1.Value = "" Then
MsgBox "Please enter the Stuff", vbCritical
Exit Sub
End If
If Me.Texremr1.Value = "" Then
MsgBox "Please enter the Comment", vbCritical
Exit Sub
End If
If Me.Textdenu.Value = "" Then
MsgBox "Please enter the Deposits_Number", vbCritical
Exit Sub
End If
If Me.Textattech.Value = "" Then
MsgBox "Please enter the Attchment_File", vbCritical
Exit Sub
End If
If Me.bra1.Value = "" Then
MsgBox "Please enter the Branch", vbCritical
Exit Sub
End If
If Me.depf.Value = "" Then
MsgBox "Please enter the Deposits_For", vbCritical
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''''
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim qry As String
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Database.accdb"
If Me.txtId.Value <> "" Then
qry = "SELECT * FROM Public_Deposits WHERE ID = " & Me.txtId.Value
Else
qry = "SELECT * FROM Public_Deposits Where ID = 0"
End If
rst.Open qry, cnn, adOpenKeyset, adLockOptimistic
If rst.RecordCount = 0 Then
rst.AddNew
End If
rst.Fields("Transaction_Date").Value = VBA.CDate(Me.txtdate1.Value)
rst.Fields("Campany").Value = Me.txtcampany1.Value
rst.Fields("Type_Transaction").Value = Me.txttrans1.Value
If Me.txtdebit.Value <> "" Then rst.Fields("Debit").Value = Me.txtdebit.Value
If Me.txtcredit <> "" Then rst.Fields("credit").Value = Me.txtcredit
rst.Fields("By_Bank").Value = Me.txtbank1.Value
rst.Fields("Stuff").Value = Me.txtStuff1.Value
rst.Fields("Comment").Value = Me.Texremr1.Value
rst.Fields("Deposits_Number").Value = Me.Textdenu.Value
rst.Fields("Branch").Value = Me.bra1.Value
rst.Fields("Deposits_For").Value = Me.depf.Value
rst.Fields("UpdateTimestamp").Value = VBA.Now
rst.Update
Me.txtdate1.Value = ""
Me.txtcampany1.Value = ""
Me.txttrans1.Value = ""
Me.txtdebit.Value = ""
Me.txtcredit.Value = ""
Me.txtbank1.Value = ""
Me.txtStuff1.Value = ""
Me.Texremr1.Value = ""
Me.Textdenu.Value = ""
Me.bra1.Value = ""
Me.depf.Value = ""
MsgBox "Updated Successfully", vbInformation
Call Me.List_box_Data
End Sub

Access How do i hard code in a username and password

I am not a coder, but I enjoy tinkering.
I have a access database and I have a login screen, but I want to hard code in a high level username and password into the actual code. The username will be "Developer" the password initially will be "One"
This is what I am doing currently. any assistance would be greatly appreciated.
Private Sub cmdLogin_Click()
On Error GoTo cmdLogin_ClickErr
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Select * From TLKPeople Where Username = '" & Me.txtUserName & "' And Password = '" & Me.txtPassword & "'")
If Not rs.EOF Then
TempVars.Add "UserName", rs!UserName.Value
TempVars.Add "Password", rs!Password.Value
TempVars.Add "Admin", rs!Admin.Value
TempVars.Add "ReadOnly", rs!ReadOnly.Value
TempVars.Add "StdUser", rs!STDUser.Value
If Nz(TempVars!UserName, 0) = "Developer" Then
DoCmd.ShowToolbar "Ribbon", acToolbarYes
End If
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "FRMMenuMain"
DoEvents
Else
MsgBox "Your login as failed!", vbOKOnly, "Login Failed"
End If
rs.Close
Set rs = Nothing
Exit Sub
cmdLogin_ClickErr:
MsgBox ("Err: " & Err.Number & " " & Err.Description)
End Sub
Based on that, this should at least get your started. You will probably need to tweak it a little.
Private Sub cmdLogin_Click()
On Error GoTo cmdLogin_ClickErr
If Len(Me.txtUserName) = 0 And Len(Me.txtPassword) = 0 Then
TempVars.Add "UserName", "Developer"
TempVars.Add "Password", "One"
Else
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM TLKPeople WHERE Username='" & Me.txtUserName & "' And Password='" & Me.txtPassword & "'")
If Not rs.EOF Then
TempVars.Add "UserName", rs!UserName.Value
TempVars.Add "Password", rs!Password.Value
TempVars.Add "Admin", rs!Admin.Value
TempVars.Add "ReadOnly", rs!ReadOnly.Value
TempVars.Add "StdUser", rs!STDUser.Value
Else
MsgBox "Your login as failed!", vbOKOnly, "Login Failed"
Exit Sub
End If
rs.Close
Set rs = Nothing
End If
If Nz(TempVars!UserName, 0) = "Developer" Then
DoCmd.ShowToolbar "Ribbon", acToolbarYes
End If
DoCmd.Close acForm, Me.Name
DoCmd.OpenForm "FRMMenuMain"
DoEvents
Exit Sub
cmdLogin_ClickErr:
MsgBox ("Err: " & Err.Number & " " & Err.Description)
End Sub
Note: You may want to use an Or instead:
If Len(Me.txtUserName) = 0 Or Len(Me.txtPassword) = 0 Then

MSAccess login form

I created a login form but realized that i can log in without entering a password. only a user name. what should i do so as to solve the problem
Option Compare Database
Option Explicit
Private Sub cmdlogin_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Account Details", dbOpenSnapshot, dbReadOnly)
rs.FindFirst "UserName='" & Me.txtusername & "' "
Me.lblusername.Visible = False
If rs.NoMatch = True Then
Me.lblusername.Visible = True
Me.txtusername.SetFocus
Exit Sub
End If
Me.lblpassword.Visible = False
If rs!Password <> Me.txtpassword Then
Me.lblpassword.Visible = True
Me.txtpassword.SetFocus
Exit Sub
End If
DoCmd.OpenForm "Dashboard"
DoCmd.Close acForm, Me.Name
End Sub
If txtusername or txtpassword is Null the conditional will fail. Try:
rs.FindFirst "UserName='" & Nz(Me.txtusername,"") & "'"
If rs!Password <> Nz(Me.txtpassword, "") Then
Or consider:
Dim rs As Recordset
With Me
.lblusername.Visible = False
.lblpassword.Visible = False
If IsNull(.txtusername) Or IsNull(.txtpassword) Then
MsgBox "Enter username or password"
Else
Set rs = CurrentDb.OpenRecordset("SELECT UserName, Password FROM [Account Details] " & _
"WHERE UserName='" & .txtusername & "'", dbOpenSnapshot, dbReadOnly)
If Not rs.BOF And Not rs.EOF Then
If rs!Password <> .txtpassword Then
.lblpassword.Visible = True
.txtpassword.SetFocus
Else
DoCmd.OpenForm "Dashboard"
DoCmd.Close acForm, .Name
End If
Else
.lblusername.Visible = True
.txtusername.SetFocus
End If
End If
End With

ListBox Retaining Selection Data after Requery

I am currently working with a list box that deletes values containing the selected text from a table. I have the following code:
Private Sub DeleteEntry_Click()
Dim DeleteTbl As String
Dim Msg As String
If IsNull(Me.lstSolution) = True Then
MsgBox "Please Select a Entry", vbOKOnly, "No Entry Selected"
Else
DeleteTbl = MsgBox(Msg, vbYesNo, "Delete Entries?")
Msg = "Are you sure you wish to delete all entrys containing: "
Msg = Msg & Me.lstSolution
DeleteTbl = MsgBox(Msg, vbYesNo, "Delete Entries?")
End If
If DeleteTbl = vbYes Then
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * FROM [Solutions] WHERE [Solutions].SolutionText = '" & Me.lstSolution & "'"
DoCmd.SetWarnings True
Else
'Do Nothing
End If
Me.lstSolution.Requery
End Sub
The problem i have is that after the entry clears from the table, and i click the button again (without selecting anything) the previously selected value appears in the MsgBox still. I need to clear this value so that an error message box appears, saying that no entry has been selected
I have managed to solve the issue by adding in a NotSelected variable as follows:
Private Sub DeleteEntry_Click()
Dim DeleteTbl As String
Dim Msg As String
Dim NotSelected As String
If IsNull(Me.lstSolution) = True Then
MsgBox "Please Select a Entry", vbOKOnly, "No Entry Selected"
NotSelected = True
Else
Msg = "Are you sure you wish to delete all entrys containing: "
Msg = Msg & Me.lstSolution
DeleteTbl = MsgBox(Msg, vbYesNo, "Delete Entries?")
NotSelected = False
End If
If NotSelected = True Then
End
ElseIf DeleteTbl = vbYes Then
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * FROM [Solutions] WHERE [Solutions].SolutionText = '" & Me.lstSolution & "'"
DoCmd.SetWarnings True
End If
Me.lstSolution.Requery
End Sub
ListBox.Requery does not refresh the ListBox.Value, nor Listbox.Column(index)
You must call lstSolution.SetFocus after lstSolution.Requery. That propagates new values from current recordset to listbox columns.
I'm not sure but I think it is a bug in MS Access.

How to protect a button with a password?

I would like to know how I can protect a button with a password before run the macro?
I tried the following code, which works good, but I couldn't exit in case of the password is incorrect for the third time.
strPassword = "HR"
For lTries = 1 To 3
strPassTry = InputBox("Enter Password please", "RUN MACRO")
If strPassTry = vbNullString Then
Exit Sub
End If
bSuccess = strPassword = strPassTry
If bSuccess = True Then Exit For
MsgBox "Password incorrect"
Next lTries
#SLaks comment aside...
strPassword = "HR"
For lTries = 1 To 3
strPassTry = InputBox("Enter Password please", "RUN MACRO")
If strPassTry = vbNullString Then
Exit Sub
End If
bSuccess = strPassword = strPassTry
If bSuccess Then Exit For
MsgBox "Password incorrect"
Next lTries
If Not bSuccess Then
Msgbox "Wrong password supplied!"
Exit Sub
End If