By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,619 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,619 IT Pros & Developers. It's quick & easy.

current user name display on form

P: 3
Good day
I created a database in Access 2010 and now after opening it in Office 365 i found some of the functions previously done not working properly. I would be happy if I can get some help sorting out my problems.
1. I would like to display the current user of the database based on the person's User Login and not the Environ (computer's name). The fields in my table are User Name, User Login, Password, User Security. Below is the code i currently have

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim Security As String
  5. Me.TxtUser = Environ("UserName")
  7. If IsNull(DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.TxtUser & "'")) Then
  8.    MsgBox "No User Security set up for this user. Please Contact Admin", vbOKOnly, "login info"
  9.    Me.CmdAdmin.Enabled = False
  10. Else
  11.     Security = DLookup("UserSecurity", "tblUser", "[UserLogin] = '" & Me.TxtUser & "'")
  12.     If Security = "Admin" Then
  13.     Me.CmdAdmin.Enabled = True
  15.     Else
  16.     Me.CmdAdmin.Enabled = False
  17.   End If
2. I am interested in having a field on the data form / datasheet auto populate and save with the name of the person inputting the information.
Feb 14 '19 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 2.5K+
P: 3,284
First, I would highly recommend against you saving any password information in your tables.

Second, exactly what is it about using Environ(“UserName”) that does not give you your desired result?

Also, you may want to take a look at this article on setting User Permissions. It is a bit involved but if you use it as a template, it is very flexible.

Finally, to streamline what you already have, without changing anything functionally:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim Security As String
  4.     Me.TxtUser = Environ("UserName")
  6.     Security = Nz(DLookup("UserSecurity", _
  7.                           "tblUser", _
  8.                           "[UserLogin] = '" & Me.TxtUser & "'"), "")
  9.     If Security = "" Then
  10.         MsgBox "No User Security set up for this user. Please Contact Admin", vbOKOnly, "login info"
  11.         Me.CmdAdmin.Enabled = False
  12.     Else
  13.         If Security = "Admin" Then
  14.             Me.CmdAdmin.Enabled = True
  15.         Else
  16.             Me.CmdAdmin.Enabled = False
  17.         End If
  18.     End If
Hope this hepps.
Feb 15 '19 #2

P: 3
Thanks for your reply. My issue / problem with using Environ["UserName"] is that it returns the computer login name (it's a shared system with one user profile, so no matter who logs into the database Environ returns the name of the computer and not the user) rather than the name of the person who is currently using the database. How about the second item on my post, can you help in that regard?
Feb 15 '19 #3

Expert 100+
P: 1,430
Hi Carl

Try this
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  4.     Private Declare Function apiGetComputerName Lib "kernel32" Alias _
  5.     "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  7.     Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
  8.     "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  10. Function fOSUserName() As String
  11.     ' Returns the network login name
  13.     Dim lngLen As Long, lngX As Long
  14.     Dim strUserName As String
  15.     strUserName = String$(254, 0)
  16.     lngLen = 255
  17.     lngX = apiGetUserName(strUserName, lngLen)
  18.     If (lngX > 0) Then
  19.         fOSUserName = Left$(strUserName, lngLen - 1)
  20.     Else
  21.         fOSUserName = vbNullString
  22.     End If
  24. End Function
Then on any form or report add a TextBox called "TxtUserName" with the ContolSourse "= fOSUserName()".

Note the "=" sign is required in the ContreoSource.

No guarantees, but try it. If it doesn't work, I have a plan B.

Feb 15 '19 #4

Expert Mod 2.5K+
P: 3,284
@ Phil:
That will return the same computer user name variable.

@ cariboybgi:
If you take a look at the link I directed you to, it will address the issue of User Name vs. User’s Actual Name—although not in those same words. The key is to create a table that includes the computer user name, the person’s name, and any other pertinent information you may want to keep on hand concerning your users.

Concerning your second issue. We have a policy of one distinct question per thread. You are free to create a new thread for a solution to that question.

Hope this hepps.
Feb 15 '19 #5

Post your reply

Sign in to post your reply or Sign up for a free account.