suser_sname() vs. user_name()

  • Thread starter Thread starter Jeff via AccessMonster.com
  • Start date Start date
J

Jeff via AccessMonster.com

I use suser_sname() to return the name of the user that created the record.
The problem is it returns domain\userid and I just want userid. I thought
of using a substring to get around how the name appears in my forms but I
have old record in the database that are just userid without the domain.
Any suggestions? Should I just go with user_name()? I don’t like the fact
that user_name() returns dbo as the username if you’re the dbo, for example.
 
stick this little lot in a module and call it, the var username will have
the currently logged in username in it. There is several different ways of
doing this

this is one

Dim ret As Long, buffer(512) As Byte, i As Long
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
computername = "": langroup = "": UserName = "": logondomain = ""
UserName = Space(256)
cbusername = Len(UserName)
ret = WNetGetUser(ByVal 0&, UserName, cbusername)
If ret = 0 Then
UserName = left(UserName, InStr(UserName, Chr(0)) - 1)
Else
UserName = ""
End If

this will do also

environ("USERNAME")

it is better to do this than logging via SQL logins because you could have
several people using a SQL login at the same time, this way you can see the
difference based on NT Login info.
 
Alex White MCDBA MCSE said:
stick this little lot in a module and call it, the var username will have
the currently logged in username in it. There is several different ways of
doing this

this is one

Dim ret As Long, buffer(512) As Byte, i As Long
Dim wk101 As WKSTA_INFO_101, pwk101 As Long
Dim wk1 As WKSTA_USER_INFO_1, pwk1 As Long

The last two lines should be removed. They are not used and
require the definition for that WKSTA-Types.


[...]
ret = WNetGetUser(ByVal 0&, UserName, cbusername)
[...]

This sample code is missing the API-declaration for this
function.

Insert this line in the module header.:

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long


Whoever wants to use WNetGetUser to retrieve the username
should be aware of the fact that this will work only when
the user logged in to a NT-Domain, but not within a
workgroup.
this will do also

environ("USERNAME")

That will work only on NT/W2K/WXP, but not on Win9x-clients.

it is better to do this than logging via SQL logins because you could have
several people using a SQL login at the same time, this way you can see the
difference based on NT Login info.

While this may be a suitable solution in some projects,
it requires the logging to be implemented on the client,
which is IMO not the best approach.

I would rather advice to use a distinct account for each
user and log the changes via a trigger on the server.

cheers
Phil
 
Back
Top