Link to home
Start Free TrialLog in
Avatar of Tom Crowfoot
Tom CrowfootFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Dlookup username

Dear Experts.

In Access 2010 I'm struggling with a Dlookup ...

I have a Function that gets the user's network Login (GetOSUserName()) & this happily displays the login name all correctly in a text box ("UserNameTextBox").  What I am trying to do is create another text box whose data is derived from a dlookup of that username in a table called "TBL-Users".  This table contains various fields, the one i want to have returned is "FirstName"

I have got this far:

Control Source = DLookUp("FirstName","TBL-Users","[Login] = '" & [UserNameTextBox] & "")

But I just get a #Error returned

I have also tried to bypass using the [UserNameTextBox] and go straight for a lookup on the Function using

Control Source = DLookUp("FirstName","TBL-Users","[Login] = '" & GetOSUserName() & "")

But I just get a #Error returned

Can anybody help
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Try bracketing your table name:

= DLookUp("FirstName","[TBL-Users]","[Login] = '" & [UserNameTextBox] & "")


(and avoid spaces and special characters in your naming conventions)

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Crowfoot

ASKER

Fantastic - thank you very much
check this out, you where missing the closing quote.
also, you need to make your code fail-safe against sql injection:

Control Source = DLookUp("FirstName","TBL-Users","[Login] = '" & replace([UserNameTextBox], "'", "''") & "' ")
<<This table contains various fields, the one i want to have returned is "FirstName">>

  Just a word of caution; if your trying to fetch more then one or two fields from that user record, you'd be better off to open a recordset in code and only fetch the record once and have all the fields available then you will be to make repeated DLookup() calls.

Jim.