Writing current user / comp name to field

Christopherusly

Village Idiot.
Local time
Today, 01:24
Joined
Jan 16, 2005
Messages
81
When using the modUSER and modCNAME to display the current logged in user and machine name, how would i go about writing this to a table when a record is created ?

This is a great little bit of code which is fantastically useful, if only i were able to record the instance of the user creating records, which would not require the user to actually provide their name ( dishonest bunch that they are :) )

Any suggestions, or pointers in the right direction are most welcome :) Many thanks


The code looks like:

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

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

and

Option Compare Database

Private Declare Function apiGetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSMachineName() As String

Dim lngLen As Long, lngX As Long
Dim strCompName As String

lngLen = 16
strCompName = String$(lngLen, 0)
lngX = apiGetComputerName(strCompName, lngLen)

If lngX <> 0 Then

fOSMachineName = Left$(strCompName, lngLen)

Else

fOSMachineName = ""

End If
End Function
 
I typically grab that info when the app starts and store it in a hidden textbox on a main menu (always open). An append query can refer to that textbox, or if you have a bound data entry form, it could have a "User" field that had a default value referring to that textbox. Kind of depends on how you're adding records.
 
Using the Environ() function is easy to call that info.

Code:
MsgBox "Network Name: " & Environ("UserName")
MsgBox "Computer Name: " & Environ("ComputerName")
There are more available, search for Environ.
 
Set field's default value in table design to 'Environ("UserName")'
 
Set field's default value in table design to 'Environ("UserName")'
Access 2003 will not let you do that. Even if you have the Sandbox mode turned off. Previous versions of Access will let you do that.
 

Users who are viewing this thread

Back
Top Bottom