MSAccess login form - vba

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

Related

dialog form 's record is locked

I have form (Arzyabi_Tamin_Konande_da) that opens in dialog form by this code:
Me.Form.Dirty = False
Dim ASK As Integer
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
With rs
.MoveFirst
Do While Not rs.EOF
.Edit
If (!Tahvil_Tmp = True) * (!Az_Tankhah = False) Then
If DLookup("[Saff_Arzyabi_2]", "Arzyabi_Tamin_Konande_sh", _
"val([Cod_Tamin_Konande]) = '" & !Cod_Tamin_Konande & "'") = True Then
DoCmd.OpenForm "Arzyabi_Tamin_Konande_da", acNormal, , "[Cod_Tamin_Konande]=" & !Cod_Tamin_Konande, , acdialog
End If
.Update
.MoveNext
Loop
end with
but when the form gets open I cant change records, all the record get locked
other wise if I open the form in acWindowNormal mode every thing is right
I try to create another query for the loop I use but it's not working.
But why are you using a edit command in the loop that opens that form?
You have this:
With rs
.MoveFirst
Do While Not rs.EOF
.Edit <------ WHY? This does nothing?????? explain!!!
If (!Tahvil_Tmp = True) * (!Az_Tankhah = False) Then
If DLookup("[Saff_Arzyabi_2]", "Arzyabi_Tamin_Konande_sh", _
"val([Cod_Tamin_Konande]) = '" & !Cod_Tamin_Konande & "'") = True Then
DoCmd.OpenForm "Arzyabi_Tamin_Konande_da", acNormal, , "[Cod_Tamin_Konande]=" & !Cod_Tamin_Konande, , acDialog
End If
.Update
.MoveNext
Loop
End With
So, what does that .Edit command do? All it REALLY does is wind up locking the reocrd, but then that does ZERO value, does nothing of value, and you don't do any edits??? So, why? What is the reason for that .edit command? (except to lock the reocrd!!!).
Remove that edit command, you are launching some form, and that form should be able to do whatever it likes to do!!!!
A wild good guess in the dark??
That code should be this:
With rs
.MoveFirst
Do While Not rs.EOF
If (!Tahvil_Tmp = True) * (!Az_Tankhah = False) Then
If DLookup("[Saff_Arzyabi_2]", "Arzyabi_Tamin_Konande_sh", _
"val([Cod_Tamin_Konande]) = '" & !Cod_Tamin_Konande & "'") = True Then
DoCmd.OpenForm "Arzyabi_Tamin_Konande_da", acNormal, , "[Cod_Tamin_Konande]=" & !Cod_Tamin_Konande, , acDialog
End If
.MoveNext
Loop
End With
me.Refresh <---- show any update dated in our form after dialog
prompts are done.

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

Error 3421 Data Type Connection Error Multy Column Combobox

I have a scenario where in I have to save "STATUS" code into table from a "COMBO BOX". This Combo Box shows "Status ID" and "Status Description" together. But while saving I need to save only "Status ID"
Following is the code for the complete form functionality. Error is occurring on click of "SAVE" button. On line where I am assigning the value from combo to "Recordset Column" on line # 77 or 90.
' rs![status_ID] = Me.cboStatus.Column(1)
Option Compare Database
Option Explicit
Dim db As Database
Dim rs, rs2, rs3 As Recordset
Dim SQL, SQL1, SQL2 As String
Dim intChk As Integer
Private Sub btnFirst_Click()
If Not rs.BOF Then
rs.MoveFirst
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnLast_Click()
If Not rs.EOF Then
rs.MoveLast
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnNew_Click()
SQL2 = "select Max(job_ID) as JID from tbl_mst_JobOrder"
Set rs3 = CurrentDb.OpenRecordset(SQL2, dbOpenDynaset, dbSeeChanges)
If Not rs3.EOF And Not rs3.BOF Then
Me.txtJobID = rs3!JID + 1
End If
Set rs3 = Nothing
TxtSetEmpty
End Sub
Private Sub btnNext_Click()
If Not rs.EOF Then
rs.MoveNext
Set_Data
End If
If rs.EOF Then
rs.MovePrevious
End If
End Sub
Private Sub btnPrevious_Click()
If Not rs.BOF Then
rs.MovePrevious
Set_Data
End If
If rs.BOF Then
rs.MoveNext
End If
End Sub
Private Sub btnSave_Click()
Dim SQL As String
IfEmpty
Dim sqlShift As String
If intChk = 1 Then
intChk = 0
Exit Sub
Else
SQL = "select job_ID from qryJobDetails " _
& "where job_ID = " & Me.txtJobID
Set rs2 = CurrentDb.OpenRecordset(SQL)
If Not rs2.EOF Then
Dim CHK As String
Me.lblChk.Caption = rs2![job_ID]
End If
Set rs2 = Nothing
If Me.txtJobID.Value = Me.lblChk.Caption Then
Dim msgUpd, msgNew, strCobSt As String
strCobSt = Me.cboStatus.Column(1)
msgUpd = "Do you want to update Location ID " & Me.lblChk.Caption
If MsgBox(msgUpd, vbYesNo, "Location Update") = vbYes Then
rs.Edit
rs![job_Date] = Me.dtpJDate.Value
rs![job_Desc] = Me.txtJobDesc
rs![loc_ID] = Me.txtLocID
rs![status_ID] = Me.cboStatus.Column(1)
rs![Comments] = Me.txtComment
rs.Update
RefreshListBox
End If
Else
msgNew = "Do you want to add New Location"
If MsgBox(msgNew, vbYesNo, "Add New Location") = vbYes Then
rs.AddNew
rs![job_ID] = Me.txtJobID
rs![job_Date] = Me.dtpJDate.Value
rs![job_Desc] = Me.txtJobDesc
rs![loc_ID] = Me.txtLocID
rs![status_ID] = Me.cboStatus.Column(1)
rs![Comments] = Me.txtComment
rs.Update
RefreshListBox
End If
End If
End If
End Sub
Private Sub Form_Load()
Set db = CurrentDb
SQL = "Select status_ID, status_Desc from tbl_mst_Status order by status_ID"
Set rs2 = db.OpenRecordset(SQL)
Do Until rs2.EOF
Me.cboStatus.AddItem rs2![status_ID] & "|" & rs2![status_Desc]
rs2.MoveNext
Loop
Set rs2 = Nothing
Set rs = db.OpenRecordset("qryJobDetails", dbOpenDynaset, dbSeeChanges)
RefreshListBox
Set_Data
End Sub
Private Sub Set_Data()
If Not rs.BOF And Not rs.EOF Then
Me.txtJobID = rs![job_ID]
Me.dtpJDate = rs![job_Date]
Me.txtJobDesc = rs![job_Desc]
Me.txtLocID = rs![loc_ID]
Me.txtLocDec = rs![location_desc]
Me.cboStatus = rs![status_ID] & "|" & rs![status_Desc]
Me.txtComment = rs![Comments]
End If
End Sub
Private Sub RefreshListBox()
Me.lstJobOrd.RowSource = ""
Me.lstJobOrd.AddItem "Job Order" & ";" & "Job Date" & ";" & "Job Description" & ";" _
& "Loc Description" & ";" & "Loc ID" & ";" & "Sta ID" & ";" _
& "Sta Desc" & ";" & "Comments"
rs.MoveFirst
Do Until rs.EOF
Me.lstJobOrd.AddItem rs![job_ID] & ";" & rs![job_Date] & ";" & rs![job_Desc] & ";" _
& rs![location_desc] & ";" & rs![loc_ID] & ";" & rs![status_ID] & ";" _
& rs![status_Desc] & ";" & rs![Comments]
rs.MoveNext
Loop
rs.MoveFirst
End Sub
Private Sub TxtSetEmpty()
Me.txtJobDesc = ""
Me.dtpJDate = Now()
Me.txtLocDec = ""
Me.cboStatus = ""
Me.txtComment = ""
Me.txtLocID = ""
End Sub
Private Sub lstJobOrd_Click()
With Me.lstJobOrd
Me.txtJobID.Value = .Column(0)
Me.dtpJDate.Value = .Column(1)
Me.txtJobDesc.Value = .Column(2)
Me.txtLocDec.Value = .Column(3)
Me.txtLocID.Value = .Column(4)
Me.cboStatus.Value = .Column(5)
Me.txtComment.Value = .Column(7)
End With
End Sub
Private Sub IfEmpty()
Dim txtCtr As Control
Dim cboCtr As Control
Dim Str As String
Str = Empty
For Each txtCtr In Me.Controls
If TypeOf txtCtr Is TextBox Then
If IsNullOrEmpty(txtCtr) Then
txtCtr.BackColor = RGB(119, 192, 212)
txtCtr.BorderColor = RGB(157, 187, 97)
Str = Str & txtCtr.Tag & vbNewLine
Else
txtCtr.BackColor = vbWhite
txtCtr.BorderColor = RGB(192, 192, 192)
End If
End If
Next txtCtr
For Each cboCtr In Me.Controls
If TypeOf cboCtr Is ComboBox Then
If IsNullOrEmptyCbo(cboCtr) Then
cboCtr.BackColor = RGB(119, 192, 212)
cboCtr.BorderColor = RGB(157, 187, 97)
Str = Str & cboCtr.Tag & vbNewLine
Else
cboCtr.BackColor = vbWhite
cboCtr.BorderColor = RGB(192, 192, 192)
End If
End If
Next cboCtr
If IsNull(Str) Or Str = "" Then
Exit Sub
Else
MsgBox "Please enter data in the highlited fields. " & vbNewLine & _
String(52, "_") & vbCrLf & Str, vbInformation + vbOKOnly, "Data not Complete"
intChk = 1
Exit Sub
End If
End Sub
Private Sub txtLocDec_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 113 Then
DoCmd.OpenForm "frmLocSer", acNormal, , , acFormAdd, acWindowNormal
End If
End Sub`

vb6 Login form code error

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.