DLOOKUP onload

paulcraigdainty

Registered User.
Local time
Today, 09:24
Joined
Sep 25, 2004
Messages
74
I'm trying to get the following code working when a form loads:

Private Sub Form_Load()

txtQ1.Value = DLookup("[Q1]", "tblQ1")


Basically the value of txtQ1 is populated with the value of Q1 in tblQ1. When opening the form the text box doesn't populate.

It's strange becasue I can get the code to work behind a check box like this:

If chkQ5.Value = True Then

txtQ5.Value = DLookup("[Q5]", "tblQ5")

Can anyone tell me why I can't get it to work on form load?
 
It should work. Have you single stepped the code to see what is happening? What version of Access are you using and on what OS? DLookup() returns a Null if it can not find anything.
 
Is there only one record in tblQ1? I ask because you've omitted the Criteria argument for your DLookup(). When Criteria is omitted, DLookup() will return a random value for [Q1], including , I think, a Null if the particular [Q1] it returns is empty.

Also, by placing the code in the Form_Load event, it will only populate the first record in your form, which may make a difference, depending on what your form is being used for.

Other than that, I see no reason why your code shouldn't work in Form_Load.
 

Users who are viewing this thread

Back
Top Bottom