Display logged on user - Stupid question #1

  • Thread starter Thread starter Pecanfan
  • Start date Start date
P

Pecanfan

Hi,

Apologies if this is a bit of a basic question for on here, but I want to
display the current logged on user on a form. I don't want to show the
actual logon name, I want to show the real username.

e.g.
logon name: bloggsj
display name: Joe Bloggs

I *think* I know how to do this with DLookup, but is this the best way of
doing it?
i.e.
=DLookUp("EmployeeName","TBL_Employees","EmployeeLogon"=CurrentUser())

I heard DLookUps are bad...

TIA,

Andy
 
Pecanfan said:
Hi,

Apologies if this is a bit of a basic question for on here, but I want to
display the current logged on user on a form. I don't want to show the
actual logon name, I want to show the real username.

e.g.
logon name: bloggsj
display name: Joe Bloggs

I *think* I know how to do this with DLookup, but is this the best way of
doing it?
i.e.
=DLookUp("EmployeeName","TBL_Employees","EmployeeLogon"=CurrentUser())

I heard DLookUps are bad...
They can be slow with high data volumes. An alternative would be a select
SQL statement in the form's open event (untested):

dim db as DAO.Database, rs as DAO.Recordset, strSQL as string
strSQL = "Select EmployeeName from TBL_Employees where EmployeeLogon = " &
CurrentUser
set db = Currentdb
set rs = db.OpenRecordset(strSQL)
rs.MoveFirst
Me.txtMyTextBox = rs!EmployeeName
rs.Close
db.Close
set rs=Nothing
set db=Nothing

You'd have to code for things like there being no entry in the table for
CurrentUser ... maybe other instances too but can't think just off the top
of my head ... anyone?

HTH - Keith.
www.keithwilby.com
 
Pecanfan wrote:

=DLookUp("EmployeeName","TBL_Employees","EmployeeLogon"=CurrentUser())

Nope, the third parameter is not correct. Try this (spaces added for
clarity):

= DLookUp ( "EmployeeName", "TBL_Employees", "EmployeeLogon=""" &
CurrentUser() & """" )

I heard DLookUps are bad...

DLookup is fine for your purpose here.

HTH,
TC
 
=DLookUp("EmployeeName","TBL_Employees","EmployeeLogon"=CurrentUser())
Nope, the third parameter is not correct. Try this (spaces added for
clarity):

= DLookUp ( "EmployeeName", "TBL_Employees", "EmployeeLogon=""" &
CurrentUser() & """" )



DLookup is fine for your purpose here.

Cheers for the replies - noticed my typo after pressing 'send' - d'oh! The
following also seems to work:-

=DLookUp("EmployeeName","TBL_Employees","EmployeeLogon=CurrentUser()")

Any reason for the extra quotes in your version?

Cheers,

Andy
 
Any reason for the extra quotes in your version?

The third parameter to DLookup() is a string expression which, when
evaluated, is the WHERE clause for the DLookup. That is, the third
parameter should evaluate to a string, that string being a boolean
expression which in turn evaluates to true or false.

Say CurrentUser is "Fred Smith".

1. "EmployeeLogon"=CurrentUser()) is actually a /boolean/ expression
which evaluates directly to True or False, depending on whether the
current user's name is or is not "EmployeeLogon", respectively. That is
clearly not what you want! The parameter should be a /string/
expression, not a boolean one.

2. "EmployeeLogon=""" & CurrentUser() & """" is a /string/ expression
which evaluates to the following boolean expression:

EmployeeLogon="Fred Smith"

3. "EmployeeLogon=CurrentUser()" is also a string expression, which
evalues to the following different, but functionally equivalent,
boolean expression:

EmployeeLogon=CurrentUser()

HTH,
TC
 
Back
Top