?
Solved

Return NT username in access

Posted on 2003-03-18
19
Medium Priority
?
418 Views
Last Modified: 2008-02-07
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
0
Comment
Question by:dman_80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +4
19 Comments
 
LVL 1

Expert Comment

by:rveliz
ID: 8161349
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
 

Author Comment

by:dman_80
ID: 8161458
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
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 8161556
please post the code you are using for Environ("UserName"), I have used this repeatedly with no issues.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 17

Expert Comment

by:Natchiket
ID: 8161561
0
 
LVL 28

Expert Comment

by:TextReport
ID: 8161594
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
 
LVL 18

Accepted Solution

by:
1William earned 400 total points
ID: 8161600
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
 
LVL 1

Expert Comment

by:hancer
ID: 8161833
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
 
LVL 6

Expert Comment

by:PsychoDazey
ID: 8161897
The way I have used it is like so:
Dim strUser as string

strUser = Environ("UserName")
'Do what you want with the string
0
 

Author Comment

by:dman_80
ID: 8161929
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
 
LVL 18

Expert Comment

by:1William
ID: 8162005
Hey DMan, did you try the code I submitted?
0
 
LVL 1

Expert Comment

by:rveliz
ID: 8162016
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
 
LVL 28

Expert Comment

by:TextReport
ID: 8162110
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
 

Author Comment

by:dman_80
ID: 8162145
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
 
LVL 18

Expert Comment

by:1William
ID: 8165994
Well, me answer this question was a waste of time!
0
 

Author Comment

by:dman_80
ID: 8166631
will,  I tried your code too and it did nothing, gave no error or anything.
0
 
LVL 1

Expert Comment

by:rveliz
ID: 8166657
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
 
LVL 18

Expert Comment

by:1William
ID: 8166676
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
 

Author Comment

by:dman_80
ID: 8167231
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
 
LVL 18

Expert Comment

by:1William
ID: 8167307
LOL  Live and learn.  Glad we got it resolved!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

752 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