Capturing a user login name

X

xavier

When I use the" =" " & atCNames(1)"function to display
the user login name in a form it uses an unbound field,
when the user adds a record I want to capture that
current user name and save it in the record how can I
accomplish this in an easy way.

Thanks
Xavier

PS we use Windows XP & 2000 with Access XP
 
R

Rick

=currentuser




When I use the" =" " & atCNames(1)"function to display
the user login name in a form it uses an unbound field,
when the user adds a record I want to capture that
current user name and save it in the record how can I
accomplish this in an easy way.

Thanks
Xavier

PS we use Windows XP & 2000 with Access XP
 
X

Xavier

Rick

=currentuser picks up a user if your using access security
the " =" " & atCNames(1)" picks up user login name from
windows using a function through the api_getusername and
api_getcomputername that I found one day and tweeked it
till I got it to give me what I needed which is the MS
Windows network login name.

Now the trouble I have is I cant seem to save it in the
record which is where I need it.

Thanks
Xavier
 
J

Joan Wild

Xavier said:
Now the trouble I have is I cant seem to save it in the
record which is where I need it.

In the BeforeUpdate event for your form use
Me.UpdatedBy = atCNames(1)

This assumes you have added a field named UpdatedBy in your table, and that
field is included in the record source for the form (it doesn't need to
actually appear on the form).
 
R

Rick

I am using a similar function that gets the network username. The
information is called by using the function fOSUserName. I have been able
to put it into an unbound text box by using =fOSUserName() as
the control source. I have also been able to use it in vba code to place
the network name in a field.

I know that does not answer your question 100%, but maybe you can take this
and apply it to your database or figure out where the error is. If not,
hopefully others will see the post and spot your error.

The following is the code from my module:




Option Compare Database
Option Explicit

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





Rick

=currentuser picks up a user if your using access security
the " =" " & atCNames(1)" picks up user login name from
windows using a function through the api_getusername and
api_getcomputername that I found one day and tweeked it
till I got it to give me what I needed which is the MS
Windows network login name.

Now the trouble I have is I cant seem to save it in the
record which is where I need it.

Thanks
Xavier
 
G

Guest

Greetings

The following code can be used to capture a Network Login
Name, copy section 1 to a Module: -
[SECTION 1]
Option Compare Database
Public LoginUserName As String
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&)

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 or
Windows 2000
'==========================================================
========

'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/Windows 2000-specific section
'==========================================================
======

Debug.Print ComputerName, langroup, UserName,
logondomain
LogonUserName = UserName
WorkstationID = ComputerName
End Function

[END SECTION 1]

Copy this section to another Module: -

Function Login()
Call GetWorkstationInfo
End Function

Finally, at Startup, call up the function "Login". You
can now use the value of the Variable LoginUserName to
assign the users Login Name to Fields etc.

HTH


Tony C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top