Using the windows login

JvM

Registered User.
Local time
Today, 19:51
Joined
Jan 7, 2003
Messages
68
Good afternoon,

Can anybody tell me how I can get the username from windows to show in a field on my form?

Thqnkx allready
 
Do a search on users or username - this was discussed recently and I posted the code to do it

Col
:cool:
 
Put this code in a module


Code:
   ' 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

   Const NoError = 0       'The Function call was successful


Function GetUserName()

      On Error Goto Err_GetUserName

          ' Buffer size for the return string.
      Const lpnLength As Integer = 255

      ' Get return buffer space.
      Dim Status As Integer

      ' For getting user information.
      Dim lpName, lpUserName As String

      ' 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.
         GetUserName = Left$(lpUserName, InStr(lpUserName, Chr(0)) - 1)
      Else

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

Exit_GetUserName:
   Exit Function

Err_GetUserName:
    MsgBox Err.Num & ": " & Err.Description
    Resume Exit_GetUserName

End Function


and on your form, you can set the default value of your textbox to:

=GetUserName()
 
Last edited:
Thank you both for your answers but they don't work.

With the last answer I get an error on this piece.

Err_GetUserName:
MsgBox Err.Num & ": " & Err.Description
Resume Exit_GetUserName

End Function


]
 
Last edited:
Sorry, my mistake: I've edited it to be good now.
 
Thank you very much I'm going to try right now!!

Greetz,

Janneman
 
It works, it works, it works....

Thank you very much!!!

Just one slight thing...

How can I save it to a table for later use...

Oh great acces goeroe help me once more please..

Greetz Janneman
 
Good evening,

Very nice piece of acces knowledge.

Thank you very much I can use these kind of tipps.
Interesting I didn't yet use a Module. Maby strange but I'm just a beginner. But eager to learn more..

Greetings,

Jan
 
I'm not certain if this might help others or not, but I was unable to get this code to run when I set the default value as =GetUserName() or just GetUserName(), but if I set the Control Source to be =GetUserName() it worked correctly for me.
 
Kyle,

you can also use:

Code:
=Environ("username")
 

Users who are viewing this thread

Back
Top Bottom