Access Login Password Update VBA & SQL - sql

Have an Access front end Login Form which includes an option for the user to change password. In frm_Login Sub I'm attempting to use the following to pass the entered username "Me.txtUserName" to frm_PassChange:
If Me.changepass = "Yes" Then
DoCmd.OpenForm "frm_PassChange", , , , , Me.txtUserName
End If
In frm_PassChange Sub I want the user to enter a new password "Me.txtNewPass" which I will then store in a usertable X_tblUsers:
CurrentDb.Execute "UPDATE X_tblUsers SET X_tblUsers.Password = '" & Replace(Me.txtNewPass.Value, "'", "''") & "' " & _
"WHERE X_tblUsers.Username = '" & Me.OpenArgs & "'"
I'm getting a type mismatch error on the DoCmd.OpenForm call.
Can anyone help?

You need one more comma before your Me.txtUserName
Right now you're trying to pass it to the WindowMode argument
Just use Intellisense as you type and insert your commas - it'll popup the argument as you go along

Related

How to enable and disable a button on form through dlookup function?

I have a query which shows a list of users and their access level which has tick box for the edit field.
The query shows the username, Form name and Edit field.
If the Edit field is ticked then I want the user to be able to click on the edit button and makes changes to the data on the form.
I have come up with this:
If (Dlookup("Edit", "QryUserAction", "UserName ="& Me!TempVars("UserName").[Value]
And "FormName = "& Forms!Form.Name)) = False Then
Forms!FrmPatientInfo!btnEdit.Enabled = False
Else
Forms!FrmPatientInfo!btnEdit.Enabled = True
Both of these should work. Your issue is you were not using the AND part of Dlookup correctly. The AND clause needs to be in quotes as it is part of the syntax. I also assume you left out the single quotes it requires for string values.
Side note - having a control to store UserName is bad practice - there is the environ function which can get you the user name. Unless the user's logging is different than whats store din the db, this should be sufficient
If (Dlookup("Edit", "QryUserAction", "UserName ='" & Me!TempVars("UserName").[Value] & "' And FormName = '" & Me.Name & "'")) = False Then
Forms!FrmPatientInfo!btnEdit.Enabled = False
Else
Forms!FrmPatientInfo!btnEdit.Enabled = True
End if
If (Dlookup("Edit", "QryUserAction", "UserName ='" & ENVIRON("USERNAME") & "' And FormName = '" & Me.Name & "'")) = False Then
Forms!FrmPatientInfo!btnEdit.Enabled = False
Else
Forms!FrmPatientInfo!btnEdit.Enabled = True
End if

Filter SubForm based on multiple criteria

I have a Form with a SubForm, i'm trying to filter based on a range of two dates and an Username (i take the criteria from 3 textboxes), the part of the dates works fine but when i press the button to start the filter, a pop-up shows saying "Enter parameter value" (even if i type the username in the textbox), i enter an username and filters correctly, but the parameter its kept and the filter won't work anymore, until i close the Form and open again, any ideas?
I don't know why ask me to enter a parameter if i have already the username in the textbox.
Here my code:
Private Sub Filter_Click()
Dim QIL As Form
Set QIL = Forms("QIL")
If IsNull(Me.username_textbox) Or IsNull(Me.date_from_textbox) Or IsNull(Me.date_to_textbox) Then
MsgBox "Insert date or username"
Else
With Me.Superlinks_subform.Form
.Filter = "[Date] = #" & Format(Me.date_from_textbox, "mm\/dd\/yyyy") & _
"# AND #" & Format(Me.date_to_textbox, "mm\/dd\/yyyy") & "# AND [User] = " & Me.username_textbox.Value & ""
.FilterOn = True
End With
End If
End Sub
Regards
Diego.
Assuming the field [User] is a String - include single quotes like this:
[User] = '" & Me.username_textbox.Value & "'"

Opening a report with multiple criteria

Can someone please guide me on this? When I use:
If Not IsNull(Me.filterLocation) And Not IsNull(Me.filterArea) Then
If Application.CurrentProject.AllReports("rptFiltered").IsLoaded =
False Then
DoCmd.OpenReport "rptFiltered", acViewPreview, , (("ColumnLocation =
'" & Me.filterLocation.Value & "'") And ("ColunmArea = '" &
Me.filterArea.Value & "'"))
Exit Sub
End If
I get a syntax error with the code above. It works if I remove:
And ("ColunmArea = '" & Me.filterArea.Value & "'"))
But I also need records with this column values to also show on the report.
There are a few issues with your code:
If a single expression traverses onto a new line, you will need to suffix the preceding line with a line continuation symbol, which, in VBA, is the underscore character (_) e.g.:
DoCmd.OpenReport "YourReport", _
acViewNormal
The and should be included in the where condition:
DoCmd.OpenReport "rptFiltered", acViewPreview, , _
"ColumnLocation = '" & Me.filterLocation.Value & "' AND ColumnArea = '" & Me.filterArea.Value & "'"
You can use the syntax highlighting of any code editor to help you to determine the elements of the code which form a string, and which lie outside of the string.
You have a typo in the name of one of your fields:
ColunmArea
' ^---------- I'm guessing this should be ColumnArea
Finally, the parentheses surrounding the where condition argument are not recommended (except when you want to force an argument to be passed by value).

VBA Access Dlookup login and password from two tables

I'm having issues with a login menu that I am creating for my database. For this database I have the location of the User login email and password in two locations. After I solve this issue, i'll make validate where the login details originated from to dictate which forms open, for now I have one form to open,
For now I just want to confirm if the the user logins and passwords are valid from either table. However it can only validate the user Login and Password from from tblMembers. If I try to enter details from tblTrainers, I would keep getting a mismatch error. I am aware what this error but not too sure how it works here.
However if I get rid off the Or statement close the statement, it works but of course I cannot use login details from tblTrainers to login. Could anyone offer any suggestions please? Code found below.
Private Sub Command1_Click()
If IsNull(Me.txtLoginID) Then
MsgBox "Please Enter LoginID", vbInformation, "Required"
Me.txtLoginID.SetFocus
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please Enter A Password", vbInformation, "Required"
Me.txtPassword.SetFocus
Else
If (IsNull(DLookup("Member_Email", "tblMembers", "Member_Email = '" & Me.txtLoginID.Value & "' And Member_Password = '" & Me.txtPassword.Value & "'")) Or (DLookup("Trainer_Email", "tblTrainers", "Trainer_Email = '" & Me.txtLoginID.Value & "' And Trainer_Password = '" & Me.txtPassword.Value & "'"))) Then
MsgBox "Inccorect LoginID or Password"
Else
DoCmd.OpenForm "mnuMain_Menu"
DoCmd.Close acForm, "frmLogin"
End If
End If
End Sub
You can use CurrentDb.OpenRecordset to open recordsets based on SQL queries. You can use .EOF to check if the recordset is at the end of the file, thus contains 0 records.
If you want to query multiple tables at once, you can use a UNION query for that.
If CurrentDb.OpenRecordset("SELECT 1 FROM tblMembers WHERE Member_Email = '" & Me.txtLoginID.Value & "' And Member_Password = '" & Me.txtPassword.Value & "' UNION ALL SELECT 1 FROM tblTrainers WHERE Trainer_Email = '" & Me.txtLoginID.Value & "' And Trainer_Password = '" & Me.txtPassword.Value & "'").EOF Then
Note that this login code is at risk for SQL injection, and these kind of login forms are fundamentally insecure. You can easily demonstrate SQL injection by entering ' OR 1 = 1 OR '' = ' as a username, and entering a random character in the password field. That passes as a valid login if there are entries in the table. An easy fix for SQL injection is to use parameters.

Export from Excel to SQL: Login Failed for User

I'm trying to create a function within Excel that will write data to my remote SQL Server database with a click of a button. However, I keep getting an error telling me that that my login failed for user "UserName" (Error 80040e4d).
At first I thought there must be a problem with my User/Login within the database, so I went to the remote desktop, opened SQL Server Management Studio and checked all my permissions, made sure Windows authentication was selected, checked the password etc... Still didn't work.
My User has been given Read and Write permissions, so I tried to import data into Excel from SQL, which did work. So I'm assuming this must be an issue within my VBA code, more specifically, my connection string.
I would appreciate it if someone could take a look over it, and tell me what I'm doing wrong? Or if someone else has had this issue and knows what's causing it?
Here's the code I have:
Sub Button3_Click()
Dim conn As New ADODB.Connection
Dim iRowNo As Integer
Dim sArtistId, sForename, sSurname, sNationality, sBirthDate, sDeathDate As String
With Sheets("NewArtist")
'Open a connection to SQL Server
conn.Open "Provider=SQLOLEDB;Data Source=tcp:IPADDRESS,1433\SqlServer2008;" & _
"Initial Catalog=DatabaseName;User ID=UserName;Password=PassWord" & _
"Integrated Security=SSPI;"
'Skip the header row
iRowNo = 2
'Loop until empty cell in CustomerId
Do Until .Cells(iRowNo, 1) = ""
sCustomerId = .Cells(iRowNo, 1)
sFirstName = .Cells(iRowNo, 2)
sLastName = .Cells(iRowNo, 3)
'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "insert into dbo.Components (ArtistId, Forename, Surname, Nationality, BirthDate, DeathDate) values ('" & sArtistId & "', '" & sForename & "', '" & sSurname & "', '" & sNationality & "', '" & sBirthDate & "', '" & DeathDate & "')"
iRowNo = iRowNo + 1
Loop
MsgBox "Artists imported."
conn.Close
Set conn = Nothing
End With
End Sub
Thank you very much!
TW
Integrated Security=SSPI
this parameter in your connection string means you are going to pass your windows principal name as authentication, so the sql login and password are being ignored.
you should remove this name/value pair from the connection string, if you want to use a specified username and password.