docmd and dLookUp

c1201d

New member
Local time
Today, 05:17
Joined
Dec 22, 2011
Messages
4
I tried to use dlookup on my login form with a very simple 2 text fields UserName/PassWord table (Login). It has only 3 records (3 sets of user name and password) But dLookup keeps returning the password from the first record no matter what and which username I typed in (or uName values is).
pW = DLookup("PassWord", "Login", "UserName=" & "uName")

Is it something I did wrong?

Also, all the docmd functions are not responding (as if they're not there). for example, DoCmd OpenForm, or DoCmd Close... do I need to set up something before I could use the docmd?

Please help. Thanks
 
Where does uName come from? You haven't shown all of the code it would appear.

Assuming that uName is the name of a text box on the form, then it should be:

pW = Nz(DLookup("PassWord", "Login", "UserName=" & Chr(34) & Me.uName & Chr(34)),vbNullString)

The quotes (Chr(34)) are needed for text. You can use single quotes (') or triple double quotes (""") if you wish but I find the Chr(34) easier to read and keep it nice and clean.

The Me is a programming shortcut which refers to the current class object (in this case the form) and then I assumed your uName was a text box on the form. But if it was a variable then you would leave off the Me. part.

Oh, and I added the NZ part to handle any nulls that might comeback if there is no match otherwise you will get an INVALID USE OF NULL error.
 
Thanks Bob. the DLookUp works now!

do you know why my Docmd methods not doing anything? I was trying to close the login form and open another form after a successful login .
 
do you know why my Docmd methods not doing anything? I was trying to close the login form and open another form after a successful login .
Would need to see the code for them.
 
I figured out. I did Open Form, then Close Form. that's why the new form never stayed in the window. so It appeared that Docome didn't work. Now I Close Form first, then Open Form. Now it did what I expected. close the login form and open the next form. Thanks again for your help.
 

Users who are viewing this thread

Back
Top Bottom