Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dlookup username

Posted on 2011-10-19
5
Medium Priority
?
630 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:correlate
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36992019
Try bracketing your table name:

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


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

0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 36992025
Missing an end quote, too:

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

Author Closing Comment

by:correlate
ID: 36992034
Fantastic - thank you very much
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36992044
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], "'", "''") & "' ")
0
 
LVL 58
ID: 36992337
<<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.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question