Win95 / Access97 Need Network User name (1 Viewer)

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
Win95 / Access97 Need Network User name ~ SOLVED~

Good afternoon~

After a few searches and copy/paste attempts, it seems I am unable to accomplish my new task: To obtain the network userID of the current user.

The codes I used may not work very well with Windows95 and Access 97 (Yes, pity me). I've tried the popular fosUser() module, but methinks that's designed for NT.

Yoir suggestions are, as always, very much appreciated.
 
Last edited:

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
It is my understanding and experience that CurrentUser() works with the Access UserID info.

I wish to utilize the Network ID information.

If I am mistaken, please educate me.
 

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
My apologies Autoeng, in my first post I failed to spell out it was the fosusername() function I was using.
-----------------------------------------------------------
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)
Else
fOSUserName = ""
End If
End Function

--------------------------

One thing I have noticed and not sure if it's relevant, I did a quick view on my advapi32.dll and did not see a line for "GetUserNameA". Listed are GetProcessHeap, GetProcAddress, LoadLibraryA, GetModuleHandleA.

Should I use GetProcAddress or GetModuleHandleA instead of GetUserNameA?

Suggestions are welcomed and appreciated.
 

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
More investigation revealed:

This is from the Microsoft Support pages. I've tried using this, however the text box on my form always returns the "#Name?" error.

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q148835

Option Explicit
Type WKSTA_INFO_101
wki101_platform_id As Long
wki101_computername As Long
wki101_langroup As Long
wki101_ver_major As Long
wki101_ver_minor As Long
wki101_lanroot As Long
End Type

Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_logon_server As Long
wkui1_oth_domains As Long
End Type

Declare Function WNetGetUser& Lib "Mpr" Alias "WNetGetUserA" _
(lpName As Any, ByVal lpUserName$, lpnLength&)
Declare Function NetWkstaGetInfo& Lib "Netapi32" _
(strServer As Any, ByVal lLevel&, pbBuffer As Any)
Declare Function NetWkstaUserGetInfo& Lib "Netapi32" _
(reserved As Any, ByVal lLevel&, pbBuffer As Any)
Declare Sub lstrcpyW Lib "Kernel32" (dest As Any, ByVal src As Any)
Declare Sub lstrcpy Lib "Kernel32" (dest As Any, ByVal src As Any)
Declare Sub RtlMoveMemory Lib "Kernel32" _
(dest As Any, src As Any, ByVal size&)
Declare Function NetApiBufferFree& Lib "Netapi32" (ByVal buffer&)
Type the following procedure:
Function GetWorkstationInfo()
Dim ret As Long, buffer(512) As Byte, i As Integer
Dim wk101 As WKSTA_INFO_101, pwk101 As Long
Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long
Dim cbusername As Long, username As String
Dim computername As String, langroup As String, logondomain As _
String

' Clear all of the display values.
computername = "": langroup = "": username = "": logondomain = ""

' Windows 95 or NT - call WNetGetUser to get the name of the user.
username = Space(256)
cbusername = Len(username)
ret = WNetGetUser(ByVal 0&, username, cbusername)
If ret = 0 Then
' Success - strip off the null.
username = Left(username, InStr(username, Chr(0)) - 1)
Else
username = ""
End If

'================================================================
' The following section works only under Windows NT
'================================================================

'NT only - call NetWkstaGetInfo to get computer name and lan group
ret = NetWkstaGetInfo(ByVal 0&, 101, pwk101)
RtlMoveMemory wk101, ByVal pwk101, Len(wk101)
lstrcpyW buffer(0), wk101.wki101_computername
' Get every other byte from Unicode string.
i = 0
Do While buffer(i) <> 0
computername = computername & Chr(buffer(i))
i = i + 2
Loop
lstrcpyW buffer(0), wk101.wki101_langroup
i = 0
Do While buffer(i) <> 0
langroup = langroup & Chr(buffer(i))
i = i + 2
Loop
ret = NetApiBufferFree(pwk101)

' NT only - call NetWkstaUserGetInfo.
ret = NetWkstaUserGetInfo(ByVal 0&, 1, pwk1)
RtlMoveMemory wk1, ByVal pwk1, Len(wk1)
lstrcpyW buffer(0), wk1.wkui1_logon_domain
i = 0
Do While buffer(i) <> 0
logondomain = logondomain & Chr(buffer(i))
i = i + 2
Loop
ret = NetApiBufferFree(pwk1)

'================================================================
'End NT-specific section
'================================================================

debug.print computername, langroup, username, logondomain
End Function

EDIT: I would like to add that this successfully returns my log-on ifo in the debug window only. I have an unbound text box on my welcome form with the default value set to =GetWorkstationInfo() and that is what returns the "#Name?" error

:: growls at ACCESS ::
 
Last edited:

ghudson

Registered User.
Local time
Today, 06:02
Joined
Jun 8, 2002
Messages
6,195
Deirdre Marie,

Does this db give you the network info you want?
 

Attachments

  • usersnetworkinfo.zip
    14.4 KB · Views: 150

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
Thank you for your time and assistance, ghudson, however that does not seem to do as I require.

I would like to be able to "grab", if you will, the UserName information the person used to log-onto her/his computer with.

For instance, every morning after turning on my computer, I have to log-in to the server with my ID (LastName FirstInitial)and password. I would like ACCESS to simply capture the User Name information. It seems the fOSUserName() function works well with later versions of Windows and ACCESS - however that does not assist me as I am using Win 95 and Access 97 =(

Again, thank you for your time.
 

ghudson

Registered User.
Local time
Today, 06:02
Joined
Jun 8, 2002
Messages
6,195
Maybe it is your version of Widows 95 or the type of network you are logging into. We recently upgraded everybody to Windows XP so I can not state which version of Windows 95 some of our users had. I know the below works with Windows 95 and a NT server for I used it in a few db's...

Code:
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nsize As Long) As Long

Public Function GetNTUser() As String
On Error GoTo Err_strUserName
    
    Dim strUserName As String
    strUserName = String(100, Chr$(0))
    GetUserName strUserName, 100
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    GetNTUser = StrConv(strUserName, vbProperCase)
    MsgBox "Network ID = " & GetNTUser 'use this for testing
    
Exit_strUserName:
    Exit Function
    
Err_strUserName:
    MsgBox Err.Number & " " & Err.Description
    Resume Exit_strUserName
    
End Function

HTH
 

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
Thanks so much, again GHudson. I used the most recent code as a new module, however when I went to Run it, I received an error.

Compile Error
Sub or Function not defined.

It points to this line:
Public Function GetNTUser() As String
then highlights:
GetUserName strUserName, 100

Suggestions?

Again, thank you for the time you've invested thus far. Truly appreciated.
 

ghudson

Registered User.
Local time
Today, 06:02
Joined
Jun 8, 2002
Messages
6,195
Oops, my fault, I gave you the wrong declaration. Try this...

Code:
Option Compare Database
Option Explicit

Private 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_strUserName
    
    Dim strUserName As String
    strUserName = String(100, Chr$(0))
    GetUserName strUserName, 100
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
    GetNTUser = StrConv(strUserName, vbProperCase)
    MsgBox "Network ID = " & GetNTUser 'use this for testing
    
Exit_strUserName:
    Exit Function
    
Err_strUserName:
    MsgBox Err.Number & " " & Err.Description
    Resume Exit_strUserName
    
End Function

HTH
 

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
Fabulous!

This works for me when I hit "run"

Question: How to incorporate this to work say when the welcome form is loaded? When I use the On Load: GetNTUser() I receive an error.

Thank you Thank you Thank you
 

ghudson

Registered User.
Local time
Today, 06:02
Joined
Jun 8, 2002
Messages
6,195
Skip the ()

Private Sub Form_Open(Cancel As Integer)
MsgBox "Hello " & GetNTUser
End Sub

HTH
 

Deirdre Marie

Access in Excess
Local time
Today, 11:02
Joined
Sep 17, 2001
Messages
34
:: snoopy dance ::

I figured it out!

Hooray! This is certainly a fabulous Friday!

:: cheers ::


Thank you Thank you Thank you Thank you
 
L

lmcgowan

Guest
Get Workstation

ghudson


I have tried the code form th attachment downloaded above. But I also need to get the workstation name. I have tried using code form the microsoft website to get the workstation name, but it does not work.
Is there code which can be added to the example downloadable above so that I can get the workstation name as well?

Thanks

Lisa
 

Users who are viewing this thread

Top Bottom