Is it possible to know UserName in Access ala Excel?

C

Chrisso

Hi All

In Excel VBA I can do this:

MsgBox Application.UserName

This does not seem possible in Acces VB. Is there an equivalent?

Chris
 
J

Jae

Dear Chrisso,

I'm not an expert, but I know 2 ways of doing this. First is you set up a
MDW using security wizard. After that, you could use "Current User." Second
is to get the window login using the the below code.

Option Compare Database

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

Const NoError = 0 'The Function call was successful

Function GetUserName() As String

Dim LUserName As String
Const lpnLength As Integer = 255
Dim Status As Integer
Dim lpName

' Assign the buffer size constant to lpUserName.
LUserName = Space$(lpnLength + 1)

' Get the log-on name of the person using product.
Status = WNetGetUser(lpName, LUserName, lpnLength)

' See whether error occurred.
If Status = NoError Then
' This line removes the null character. Strings in C are null-
' terminated. Strings in Visual Basic are not null-terminated.
' The null character must be removed from the C strings to be used
' cleanly in Visual Basic.
LUserName = Left$(LUserName, InStr(LUserName, Chr(0)) - 1)

Else
' An error occurred.
MsgBox "Unable to get the name."
End
End If

GetUserName = LUserName

End Function

code if from Access 2003 VBA Programmer's Reference, Wrox. Wiley Publishing,
Inc.
 
J

Jae

Correction: The code above is not from the book Access 2003 VBA Programmer's
Reference. Although that book contains similar code, I realized that I got
the above code from somewhere else, which I cannot remember. Sorry for giving
out a wrong info.
 
N

Norman Yuan

It is very simple to use VBA's Environ() or Environ$() function to get
current user name/domain:

MsgBox Environ$("USERDOMAIN") & "\" & Environ$("USERNAME")
 
D

Douglas J. Steele

Yes, but it's also very simple for a nefarious user to change the values of
those variables before opening Access.

All you have to do is open a DOS box, use the SET command to change them,
then launch Access from that same DOS box.
 
D

Douglas J. Steele

Yes, they are, at least for a particular session.

Did you try the instructions I outlined? When I do that (Win XP Pro),
whatever I set USERNAME to in the DOS box will be picked up by an Access
session started in that same DOS box. The changes will not be visible
outside of that particular DOS box (and will not last once the DOS box is
closed), but it definitely will allow users to impact what's detected for
them should they want.
 
G

Guest

Yes, but it's also very simple for a nefarious user to change the values
of
those variables before opening Access.

In my copy of Excel, all you have to do is Tools, Options, General,
UserName.

(david)
 
Top