Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

win 2000 computer user name in form field. 1

Status
Not open for further replies.

TheEnigma

Technical User
May 26, 2002
69
US
Can anyone please tell me how I can get the Win 2k user name to show up automatically in a field in a form? (or even more ideally if this could work directly from the table)

The database has not had securities set up on it, so no one logs into the database.

The table name is DDChange, and the field name is simply name. I am currently utilising the "pages" section of access as I have the web page for this linked through our intranet at work. The page name is also DDChange

Ideally I would like the user name of the person opening the form to appear in this field upon opening.
 
Visual basic has an environ function that returns the value of environment strings, e.g.

environ("username")

IIIRC, you can view your environment variables via the system object in the control panel.

You cannot, however, use a VB function to specify the default value in a table.

One way to set a default value is to use the OnCurrent event in a form, like this:

private sub Form_Current()
if isnull(me.UserName) then
me.username = environ("username")
endif
end sub

 
Thanks. When I enter the code exactly as you have it above though, it comes up with the following VB error.

Compile error: Method or data member not found.

It highlights in yellow the following "Private Sub Form_Current()" and also higlight's in blue ".username"

Any ideas on what I am doing wrong? I had changed the field name to username just in case this was the problem, but I still had the same result.

 
hmm.. it's very likely complaining about 'username'. You may need to change the control name as well as the field name in order to get this to work.
 
Check out this link to my similar question.
JesseBalk's answer did it for me. I amended his/her code for my purposes. Copy this to a new module, I called mine GetUserInfo.
*****************************
Option Compare Database

Option Explicit

Private Declare Function GetComputerNameA Lib "kernel32" (ByVal lpBuffer As _
String, nSize As Long) As Long
Private Declare Function GetUserName Lib "ADVAPI32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long


Public Function UserName() As String
On Error GoTo ErrorHandler
Dim lpBuff As String * 25
Dim ret As Long, User As String
ret = GetUserName(lpBuff, 25)
User = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
UserName = User & ""

Exit Function

ErrorHandler:
'Call ErrMsg
End Function

Public Function GroupName() As String
On Error GoTo ErrorHandler
'put group-culling code here
'NOTE: it does not come from using the rest of the username and computername code, above and below!!!
'Note: apparently not available thru API calls; will try "whoami.exe /groups", in W2K Server Resource Kit

Exit Function

ErrorHandler:
'Call ErrMsg
End Function

Public Function ComputerName() As String
On Error GoTo ErrorHandler
Dim UserInfo As String * 255
Call GetComputerNameA(UserInfo, 255)
ComputerName = Left$(UserInfo, InStr(UserInfo, Chr$(0)) - 1)

Exit Function
ErrorHandler:
'Call ErrMsg

End Function
************************************
In your form, you enter "=UserName()" to capture the username.
 
beetee's code worked for me. Elegance in simplicity. What I did do is make a textbox called username which was implied, but not explicitly said...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top