Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

access vba code to get logged on user name

Posted on 2012-09-02
3
Medium Priority
?
1,847 Views
Last Modified: 2012-09-07
I have a button on the form that calls a on click sub that calls this function
if someone has a better way of getting the information straight from the sub I would appreciate it or tell me what is wrong with this function.

thanks gary

i have the following code in module1
for the line:status = WNetGetUser(lpName, lpUserName, lpnLength)
it tells me invalid outside of procedure

Option Compare Database
   Const NoError = 0       'The Function call was successful
   Const lpnLength As Integer = 255
   Dim lpName, lpUserName As String
   
 ' Declare for call to mpr.dll.
 Declare Function WNetGetUser Lib "mpr.dll" _
      Alias "WNetGetUserA" (ByVal lpName As String, _
      ByVal lpUserName As String, lpnLength As Long) As Long


   
Public GetUserName
  ' Buffer size for the return string.
     

      ' Get return buffer space.
      Dim status As Integer

      ' For getting user information.
     

      ' Assign the buffer size constant to lpUserName.
     ' lpUserName = Space$(lpnLength + 1)

      ' Get the log-on name of the person using product.
      status = WNetGetUser(lpName, lpUserName, lpnLength)

      ' See whether error occurred.
      If status = NoError Then
         ' This line removes the null character. Strings in C are null-
         ' terminated. Strings in Visual Basic are not null-terminated.
         ' The null character must be removed from the C strings to be used
         ' cleanly in Visual Basic.
         lpUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
      Else

         ' An error occurred.
         MsgBox "Unable to get the name."
         End
      End If

      ' Display the name of the person logged on to the machine.
      MsgBox "The person logged on this machine is: " & lpUserName

End Function
0
Comment
Question by:javagair
  • 2
3 Comments
 
LVL 75
ID: 38359101
Well, this is kind of the de-facto standard:

http://access.mvps.org/access/api/api0008.htm

Put the code in a standard VBA module.

This will give you the logged in Windows User Name

mx
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 38359103
If you want the actual Full Name of the person logged in, you can use this:
http://access.mvps.org/access/api/api0066.htm
0
 
LVL 59
ID: 38363404
You've got a comment on the line that pushes the vairable out to allow enough space for the name.  Outside of that, I don't see anything wrong outright. Below is the routine I use.

Jim

Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserNameA Lib "advapi32.dll" (ByVal lpBuffer As String, nSize As Long) As Long


Public Function WhoAmI(bReturnUserName As Boolean) As String

        ' Function returns either user name or computer name

        Dim strName As String * 255

10      If bReturnUserName = True Then
20        GetUserNameA strName, Len(strName)
30      Else
40        GetComputerNameA strName, Len(strName)
50      End If

60      WhoAmI = left$(strName, InStr(strName, vbNullChar) - 1)

End Function
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
When we develop an application in Ms Access 2016 we should also try to protect the queries, macros and table links. I know I may not have a permanent solution but for novice users, they will not manage to break your application. Below is the detail …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question