473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

current user name display on form

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
  3.  
  4.  
  5. Me.TxtUser = Environ("UserName")
  6.  
  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
  14.  
  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
4 5773
twinnyfo
3,653 Expert Mod 2GB
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
  3.  
  4.     Me.TxtUser = Environ("UserName")
  5.  
  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
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
PhilOfWalton
1,430 Expert 1GB
Hi Carl

Try this
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.     Private Declare Function apiGetComputerName Lib "kernel32" Alias _
  5.     "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  6.  
  7.     Private Declare PtrSafe Function apiGetUserName Lib "advapi32.dll" Alias _
  8.     "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  9.  
  10. Function fOSUserName() As String
  11.     ' Returns the network login name
  12.  
  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
  23.  
  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.

Phil
Feb 15 '19 #4
twinnyfo
3,653 Expert Mod 2GB
@ 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

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

Similar topics

1
by: Lin Ma | last post by:
Is there a way to display NT login user name in a asp page? Thanks, Lin
2
by: Homauoon | last post by:
Dear sir I want to get current user name from system (in windows applications) by using WNetGetUser function from Mpr.lib. Please help me. Thanks
18
by: djbitchpimp | last post by:
How do I retrieve the username of the currently logged in user? I basically to check if the user is logged in as root and if not, exit the program. I though about using the LOGNAME environment...
13
by: Steve Enzer | last post by:
How can I get the login name of the user currently logged into Windows as a string? Thanks, Steve Enzer
4
by: Joe | last post by:
This may actually be an IIS configuration issue but any help would be appreciated. I'd like to display some content based upon who the current user is that is accessing an internal ASP.NET...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.