471,853 Members | 916 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Create function that identifies current user's network login

I have an Access 2003 MDB database that uses the "currentuser()" function extensively (in queries and macros) to control what a particular user can see and do. In Access 2007 (which no longer uses user level security) the current user function now always returns "admin" , which pretty much breaks my database functionality. This database is used in a business environment and all PCs are connected to a common wired Novell network. I found instructions on the web on how to create a custom function that will return a current user's network login ID, which I think I can adapt to my database. However, I can not seem to get it to work.

I copied the below text in to a new module and named the module "fOSUserName". When I add a control to a form with the source as "=fOSUserName()", I get the following result: #Name?

I am new to VBA and modules so mistake may be something simple. I have read a lot of info on VBA and modules but have not learned enought to figure out my problem. Any advice?
Expand|Select|Wrap|Line Numbers
  1. ' This code was originally written by Dev Ashish.
  2. ' It is not to be altered or distributed,
  3. ' except as part of an application.
  4. ' You are free to use it in any application,
  5. ' provided the copyright notice is left unchanged.
  6. '
  7. ' Code Courtesy of
  8. ' Dev Ashish
  9. '
  10. Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
  11.     "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  13. Function fOSUserName() As String
  14. ' Returns the network login name
  15. Dim lngLen As Long, lngX As Long
  16. Dim strUserName As String
  17.     strUserName = String$(254, 0)
  18.     lngLen = 255
  19.     lngX = apiGetUserName(strUserName, lngLen)
  20.     If ( lngX > 0 ) Then
  21.         fOSUserName = Left$(strUserName, lngLen - 1)
  22.     Else
  23.         fOSUserName = vbNullString
  24.     End If
  25. End Function
Attached Images
File Type: jpg Current Network User Module.jpg (15.9 KB, 747 views)
Mar 15 '10 #1
3 7238
2,322 Expert Mod 2GB
Welcome to Bytes

Please remember to enclose your code in the code tags: [code][/code]
Try this:
Expand|Select|Wrap|Line Numbers
  1. Environ("UserName")
The Environ function will return varius envrironment variables, such as username. I also sometimes use it to get Environ("ComputerName"). It can be used for alot of other things as well.
Mar 15 '10 #2
8,827 Expert 8TB
The Function Definition must be 'Public' or else it will be out of Scope and not accessible (See Line #1). The actual API Declaration, however, can remain Private as long as the Function itself resides in the same Module as the API Declaration.
Expand|Select|Wrap|Line Numbers
  1. Public Function fOSUserName() As String
  2. ' Returns the network login name
  3. Dim lngLen As Long
  4. Dim lngX As Long
  5. Dim strUserName As String
  7. strUserName = String$(254, 0)
  8. lngLen = 255
  10. lngX = apiGetUserName(strUserName, lngLen)
  12. If (lngX > 0) Then
  13.   fOSUserName = Left$(strUserName, lngLen - 1)
  14. Else
  15.   fOSUserName = vbNullString
  16. End If
  17. End Function
Mar 17 '10 #3
32,462 Expert Mod 16PB
Function to Return UserName (NT Login) of Current User may help.
Mar 17 '10 #4

Post your reply

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

Similar topics

2 posts views Thread by Groupe Eurower | last post: by
8 posts views Thread by Mark White | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

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.