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

Create function that identifies current user's network login

P: 1
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
  12.  
  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, 616 views)
Mar 15 '10 #1
Share this Question
Share on Google+
3 Replies


TheSmileyCoder
Expert Mod 100+
P: 2,321
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

ADezii
Expert 5K+
P: 8,634
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
  6.  
  7. strUserName = String$(254, 0)
  8. lngLen = 255
  9.  
  10. lngX = apiGetUserName(strUserName, lngLen)
  11.  
  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

NeoPa
Expert Mod 15k+
P: 31,487

Post your reply

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