Access VBA: Find if computer username is an active user - ms-access

I'm using Access 2013.
I want to disable buttons on some forms based on the user that is logged into the computer.
I'm using code from Dev Ashish to determine the name of the logged in user to the computer.
The username is stored in global variable LoggedInUser
I have a table called "Users" in which I am storing the login emails for the admins who need expanded functions. This table along with other fields has the following fields:
"Email" Short Text
"Active" yes/no
I want to check if the logged in user exists in the users table and if they are marked as Active.
I then want a global boolean variable called AuthUser to hold a true/false for if the user exists in the table and is active or not.
I think this might need to be done with a dlookup or dcount, but I just can't seem to make it work - any ideas on a solution?

You haven't shown your query that doesn't work?
I can guess - and you can tell me I guessed wrong
Here's something you can play with
dim rs as DAO.Recordset
strSQL = "SELECT Username from Users WHERE [Username] = """ & LoggedInUser & """ AND [Active]"
set rs = currentdb.openrecordset(strSQL)
AuthUser = Not rs.EOF
rs.close

Related

How to get current logged in user id in access-db 2016

I'm making a students management system in access, in which I have made a signup table named as students and after that I made it's form and it's saving data in table and after that I made login form which is working fine, and after logging in it's showing personal details form (I have set that) and which also stores data, and in personal details table I have a field named student_id_fk which stores the primary key of that user which is logged in but I am confused that how to get currently logged in user id is there any way?
Simplest way would be to use Environ("username")
Alternatively you may use below function or call Windows API
Function getUsername() As String
Dim WshNetwork As Object
Set WshNetwork = CreateObject("WScript.Network")
getUsername = WshNetwork.UserName
Set WshNetwork = Nothing
End Function

Associating user with login session in VB.NET

I'm creating a project in VB.NET with a MySQL back end. The user logs in via a login system created in VB.NET and the users details are stored in the database.
I was just wondering theoretically how the user that logged in can be associated with that particular session through the life time of that session? I need this to happen so I can do further queries once the user has logged in which involves that user :)
Thanks in advance,
Robin
Save the UserID in a Session Variable (Assuming that you have pulled the Users ID from the database, and it is stored in a variable I am calling userid):
HttpContext.Current.Session.Add("UserID", userid)
Then when you want to use it on any page (Assuming that you're userid is an Integer):
Dim userid as Integer = HttpContext.Current.Session["UserID"]
or in a Query String:
Dim strQuery As String = "SELECT * FROM TABLE WHERE USERID = " _
+ HTTPContext.Current.Session["UserID"].ToString() + ";"

Filter combobox on global variable

So I have this Access 2010 database into which users must login. The username they use is saved as a global variable. I then have a form which updates a table when they enter data and click a "save" button. I am trying to set one of the comboboxes (user) that is currently linked to a column in the table to be filtered on the global variable so that each person can only enter data under their own username. Is this possible? Does anyody know how to code this? I'm a complete newbie to Access and VBA and would appreciate any help
Greets
Me
In the form_load() function of that form you should fill the combobox with the global variable. To be sure they can't edit you should disable the combobox as well.
Private Sub Form_Load()
Me.myComboBoxName = gMyGlobalVariableName
Me.myComboBoxName.enabled = false
End Sub
However I'm assuming that the combobox has two columns (id and username) of which the first one is hidden and the primary key of some table where you store all the usernames. The gMyGlobalVariableName should have stored the id, not the username itself.
You can set the row source of the combo in the load event of the form to include only the relevant rows.
Me.TheCombo.RowSource = _
"SELECT UserColumn, Etc FROM TheTable WHERE UserColumn ='" _
& TheVariable & "'"
You may also wish to ensure that the form only contains the relevant records, however, the fact that you have a save button, suggests an unbound form. In Access, save buttons are largely redundant because the default is to save a record and stopping saves is the difficult bit.
I wonder why you do not use their windows log-in user name?

I need to have a form that will show prices for some people and hide from others, but it's a listbox

I need to have a form that will show prices for some people and hide from others, but it's a listbox.
How do I do?
The only solution I found is to change the cols width, but is not really...
You could set the Listbox or combo box rowsource / recordsource details based on Windows login or Access login if you have built in security.
Some sort of If statement in conjuntion with the Login API, which you could find by googling. An example would be kind of like below (do this on the on load event maybe?) -
Dim UserName As String
UserName = GetUserName_TSB 'this is the windows login drawn from the LoginAPI function
If UserName = "XXXX" Or UserName = "XXXXX" Or UserName = "XXXXX" Or UserName = "XXXX" Then
Me.combobox.rowsource = 'Create a SELECT SQL query for whatever your tables / data is.
Else
Me.combobox.rowsource = 'Create a SELECT SQL query for whatever your tables / data is for other users
End If

Access Database Security Question

I have a database in Access 2003 that I only want certain people to be able to access.
In my database I have a table which lists the people that should be able to access the database. (Tbl_BIRT_Users). The table contains their name, ntlogin and email address. It also has an 'adminstrator' field.
My question has two parts:
1 - On opening the database how can I get it to lookup the ntlogin (environ username) of the person and ensure that that person is authorised to use the database?
2 - I need the database to look at the 'administrator' Yes/No field and grant read only access to non admins and full access to admins.
Thanks!
Use an API call to get the login name - API: Get Login name You can change environment variables while at the command prompt and then, if start Access executing from the command prompt, Access will use the spoofed environment variable.
Also there are ways of easily breaking table driven security such as the user taking the backend database home to a retail copy of Access resides, changing the values in the tables and bringing the database back to the office.
Even if you trust the users not to fiddle with their environment variables, please adopt Tony's suggestion anyway. After you add the module he linked, retrieving the user's account name is a simple call to the fOSUserName() function. It's really no more difficult than getting it from the user's environment.
But I want to add to Tony's point about "easily breaking table driven security". Your plan is to check whether the user is one of your authorized users. My suggestion is to place your back end database file in a location where only your authorized users can get at it. Use Windows file system permissions to keep everyone else out. That way you may decide you don't even need to check your table to determine whether the user is authorized. You could still use the table data to determine whether the user is an Admin or regular user. Or you might decide to keep the authorization check if it gives your managers peace of mind ... even though it doesn't really offer much security.
Could you not just do something like this
Dim rst as Recordset
Dim sql as string
sql = "SELECT * FROM Tbl_BIRT_Users WHERE ntlogin = '" & Environ("UserName") & "'"
set rst = CurrentDb.OpenRecordset(sql)
if (rst.bof and rst.eof) then
/*not a valid user*/
DoCmd.Quit
else
if not rst!Administrator then
/*make read only*/
end if
end if
rst.close
This is the Access security window dressing I use.
Public Function SecurityCode()
'* Purpose: Limits access to program
Dim sUserID As String
Dim sUserName As String
'* Determines user from Windows Login
sUserID = Environ("USERNAME")
'* Lookup on BE table of Allowed Users to verify on the list.
sUserName = DLookup("[UserName]", "tbl_AllowedUsers", "ID = '" & sUserID & "'")
If Len(sUserName) > 0 Then
'Allowed User, opens Main Switchboard
'Set global variable for Admin rights
g_Admin = DLookup("[AdminRights]", "tbl_AllowedUsers", "ID = '" & sUserID & "'")
DoCmd.OpenForm "Switchboard"
DoCmd.SelectObject acForm, "Switchboard", True
DoCmd.RunCommand acCmdWindowHide
Else
'Not on the Allowed Users list, opens to a Password Page
DoCmd.OpenForm "frm_LockPage"
DoCmd.SelectObject acForm, "frm_LockPage", True
DoCmd.RunCommand acCmdWindowHide
End If
End Function
Try something like the below:
Function RealName()
payroll = Environ("Username")
firstname = DLookup("[first name]", "[Payroll Numbers]", "[persno] = " & payroll)
lastname = DLookup("[Last name]", "[Payroll Numbers]", "[persno] = " & payroll)
If IsNull(firstname) = True Then
RealName = payroll
Else
RealName = firstname & " " & lastname
End If
End Function
You can then enter code in the form_load event to make sure it's a verified user.