Return NT username in access

using an Access form, I need to find out the username of the person currently logged into the windows NT 4.0 domain.  I've tried ...

Environ("Username"), but I don't think I'm using it correctly because it continues to return a blank string.

I do not want to assign usernames in access, it must be the winNT domain username.

I'm sure this is possible, I just don't think I'm doing it correctly,  Please advise...

Thank you,

-D
dman_80Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rvelizCommented:
This works.

Option Compare Database
Option Explicit

' 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()

      ' 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.
         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
dman_80Author Commented:
rveliz,

I'm not sure if it makes any difference, but I'm using access 2000.  I could not get your code to work.  does it need to be in a seperate module?  I set up a new module called test and called the "GetUserName" function fromt a command button and Access gave me dialog box stating that the code had an error.  It was a pretty vague dialog box.
Also tried the code in the general declarations of the form and still got an error.  

I'm really looking for advise on the Environ command, but I'll accept anything that works.  And if I'm using this code wrong, please let me know.  I'm still kind of new at all of this.

Thanks again...
0
PsychoDazeyCommented:
please post the code you are using for Environ("UserName"), I have used this repeatedly with no issues.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

NatchiketCommented:
0
TextReportCommented:
The Environ function does not return your DOMAIN user Name what it returns depends on the version of windows you are running Access in. If the client PC is Running Windows NT, 2000 or XP it will return the current users name, in Windows 95, 98 and ME I am not sure.

Try going to the command prompt and typing in SET and look for the UserName setting as it is this that is being returnded.

What version of Windows is the Client running on?

Cheers, Andrew
0
1WilliamCommented:
Try this:
Type wksta_user_info_1
    wkuil_username As Long
    wkuil_logon_domain As Long
    wkuil_logon_server As Long
    wkuil_oth_domains As Long
End Type
Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" (lpName As Any, ByVal lpUserName$, lpnlength&)

Public Function GetNTUserID() As String
Dim ret As Long
Dim buffer(512) As Byte
Dim cbusername As Long
Dim username As String

    username = ""
    username = Space(256)
    cbusername = Len(username)
    ret = WNetGetUser(ByVal 0&, username, cbusername)
    If ret = 0 Then
        GetNTUserID = Left(username, InStr(username, Chr(0)) - 1)
    Else
        GetNTUserID = ""
    End If

End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hancerCommented:
I tried environ username in an access form.
I place a command button in a form and write the code under the command button. That was


Private Sub Command0_Click()
  MsgBox Environ("username")
  MsgBox Environ("COMPUTERNAME")
  MsgBox Environ("USERDOMAIN")
End Sub

It works in Windows XP professional and Office XP

So whenever you want this value you write Environ("username")

Hope this example will help you
0
PsychoDazeyCommented:
The way I have used it is like so:
Dim strUser as string

strUser = Environ("UserName")
'Do what you want with the string
0
dman_80Author Commented:
ok,

I'm running windows 98 logged into an NT domain server.  the set command at the prompt does not display anything to the tune of username, computername, etc.  I think this is why Access is returning a blank sting.  I tried the Environ command on a setting that was in the set list  at the command prompt (temp) and access returned the correct value.  So I guess a new question is posed...   Why am I not showing username, computername, etc. ???  

All your help is greatly appreciated,
0
1WilliamCommented:
Hey DMan, did you try the code I submitted?
0
rvelizCommented:
Copy/paste the code I posted and it should work.  Not sure why you got an error.  If you want to use environ(), try Hancer's method to test it out.  I just tried it and it worked here (NT 4 system).
0
TextReportCommented:
Windows 95, 98 and I think ME does not store the environemtn variable in the same way as Windows NT, 2000 and XP, In NT 2000 and XP you can always rely on the Environment Varaibal; as you can not change the value of this main command process.

With Windows 95, 98 and ME I think the code posted initially by rveliz should work.

Further details can be found on this in the Microsoft Knowledgebase, Article number 210088 titled ACC2000: How to Retrieve Workgroup Information Under Win32

Cheers, Andrew
0
dman_80Author Commented:
rveliz,

I could be using your code wrong.  All I'm doing is pasting it into the general declarations of the form and then calling the function from a command button. Every time I try it I get a Dulication error.
0
1WilliamCommented:
Well, me answer this question was a waste of time!
0
dman_80Author Commented:
will,  I tried your code too and it did nothing, gave no error or anything.
0
rvelizCommented:
Make sure you are do not have "Option Explict" and "Option Compare Database" more than once in your module.  This would give you a Compile error: Duplicate Option.
0
1WilliamCommented:
Sorry for being snippy, but you gave no indicationof having read or trying it.  Odd that it did not work for you.  I have tested it on W95,W98,NT 3.5, 4.0, W2000, XP and it always works.  I'll assume you pastred all the code in a module.  Add this to the module and retest, please.

private sub Testo()
     msgbox GetNTUserID
end sub
0
dman_80Author Commented:
will, you are the man.  I had your code in it's own module and was calling the function in a command button, but not displaying it.  Sometimes I feel pretty dumb, but your code works like a charm and thanks to everyone for all you help.  
0
1WilliamCommented:
LOL  Live and learn.  Glad we got it resolved!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.