VBA for NT USERID (1 Viewer)

bessej43

Registered User.
Local time
Today, 04:21
Joined
Jun 12, 2002
Messages
22
Need help in assigning the NT USERID. In Office 97 I wrote the program this way:

Private Sub Form_Open(Cancel As Integer)
' Minimize the database window and initialize the form.

' Move to the switchboard page that is marked as the default.
Me.Filter = "[ItemNumber] = 0 AND [Argument] = 'Default' "
Me.FilterOn = True
' Function GetNtId() As String
Dim NtUsrId As String
Dim indx As Integer
indx = 1
Do
EnvString = Environ(indx)
If InStr(EnvString, "USER=") > 0 Or InStr(EnvString, "USERNAME=") > 0 Then
NtUsrId = Trim$(Mid$(EnvString, InStr(EnvString, "=") + 1, 15))
Exit Do
Else
indx = indx + 1
End If
Loop Until EnvString = ""
GetNtId = NtUsrId
End Function

Access 200 does not like the string EnvString = Environ(indx)

Is there another way to get the NT USERID?
 

ghudson

Registered User.
Local time
Today, 04:21
Joined
Jun 8, 2002
Messages
6,195
Copy this into a new module...

Public Declare Function GetUserName Lib "AdvAPI32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetNTUser() As String
On Error GoTo Err_GetNTUser

Dim strUserName As String

strUserName = String(100, Chr$(0))
GetUserName strUserName, 100
strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
GetNTUser = StrConv(strUserName, vbProperCase)

Exit_GetNTUser:
Exit Function

Err_GetNTUser:
MsgBox Err.Number, Err.Description
Resume Exit_GetNTUser

MsgBox GetNTUser 'test

End Function

HTH
 

lynsey2

Registered User.
Local time
Today, 09:21
Joined
Jun 18, 2002
Messages
439
or as my wounderful friend Hay! gave me.....Put this in a module

Option Compare Database

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String
'Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If lngX <> 0 Then
fOSUserName = Left$(strUserName, lngLen - 1)
ElsefOSUserName = ""
End If
End Function
 

bessej43

Registered User.
Local time
Today, 04:21
Joined
Jun 12, 2002
Messages
22
Thank you

GHudson,

Thank you, it worked like a charm :)

Lynsey2,

Thank you for replying to post.

All I find this site to be the best and thank all the people you have posted information or replied to post.

Jbatey
 

sarahb845

Registered User.
Local time
Today, 01:21
Joined
Jun 19, 2002
Messages
43
Thank you so much for this. I was thinking this might be an impossible thing, but it was SO easy!

sarah
 

Users who are viewing this thread

Top Bottom