Discussion Locked

Capturing Log-In ID

0 helpful
12 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have an Access 2000 Form (named ?PORmaint?) with a text field named UID (for user ID) on the form. Every time a user creates a new record I want Access to reach into the operating system, grab the Log-In User ID and paste its value into the records ?UID? field. I eventually need this to operate on any Windows and/or OS/2 operating system as well as Unix. The task should be reasonably simple for any of you MCSE type ?gurus? that have a limited knowledge of Access and/or VB but I have no clue where to begin. I would be extremely grateful to anyone providing commented code or pointing me toward a reference I can download. (this is how I will identify employees submitting a Purchase Order Request to the Purchasing Department over an intranet within an application module I am currently building) Thanks in advance - Don Quigg at

12 replies

by bob sellman

Here's a part of the answer to your question.

Create a module (or add to one) the following:
PUBLIC txtUserID as text

This will create the text variable txtUserID that will be available everywhere in your application.

I don't know how to get the login ID from the operating system, so let's assume you create a login form that starts when the user starts the application and matches the ID and password against a table within your application. If there is a match, open your application'smain form after the following (assuming UserIDControl refers to the control on your login form containing the user ID):

txtUserID = me.UserIDControl

On your input form set the default value of a hidden text control (we'll call it currentUserID) on the form equal to the public valiable txtUserID. (The control currentUserID will have its data source set to UID.) The user id will then be saved with the rest of the new record.

Contact me if you are using this solution and need more specific code, including the login to Access. (Note that I am not using the Access security system.)

I'm also very interested in learning if User IDs can be obtained from Netware 3/4/5 and Windows NT/2000 operating systems.

by donq

Sorry Bob, I need some code to pluck Log-In ID's that would work for local and/or internet users outside of our security system when they order something. I have used Access security sustems similar to the one u proposed many times and they work fine - but suffer from tunnelvision. Thanks anyhow. Don Quigg at databyte@mindspring.com

by tclere

This may be tough to read because they changed the format of the postings. They removed all line feeds and tabs which makes posting readable code almost impossible.

To get the user ID in windows using VB:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Dim ret As Long
Dim lpBuff As String * 25
Dim sUserInitials as String

ret = GetUserName(lpBuff, 25)

sUserInitials = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
sUserInitials = UCase(sUserInitials)

Hope this helps. I have the 'Notification' marked in case you need any more help with this solution.

by tclere

Oops, spoke too soon! It looks like they fixed the posting format since the last time I posted source code.

by joetech3

Use the Currentuser function. Create an update query and a macro to use this to update tyour field and tie it to the AfterUpate event.

by sheryl v

You Need the declaration & the function. Set the default value to "=GetUserName()
CODE:

Declare Function DeclareGetLogin Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function GetUserName() As String
On Error GoTo Err_GUN
Dim lngLen As Long
Dim strBuf As String

Const MaxUserName As Integer = 255
strBuf = Space(MaxUserName)
lngLen = MaxUserName
If CBool(DeclareGetLogin(strBuf, lngLen)) Then
GetUserName = Left$(strBuf, lngLen - 1)
Else
GetUserName = ""
End If

Exit_GUN:
Exit Function

Err_GUN:
msgbox Err.Description
Resume Exit_GUN

End Function

by mikeb

In an application of this type, you can get into some serioius problems if your network security settings allow this.

Here is a work-around that I have used:
create another table that includes UID, password and any other fields you need then have the user login separately against this table to gain access to the database, then any of the fields in this table are available to use any way you want. I know this is inconvenient to require users to log in twice, but there is always a tradeoff between convenience and security.

by donq

Not at all intuitive + CurrentUser works great with 1 short line of code!

This discussion is closed to new replies.