DLookup in Forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have a textbox in which I enter a Person Pay ID, I want to try and auto
populate several other textboxes with Name and Workplace, I understand I need
to use a DLookup, but unsure on how it works

How can this be done?

Jez
 
Hi, I have a textbox in which I enter a Person Pay ID, I want to try and auto
populate several other textboxes with Name and Workplace, I understand I need
to use a DLookup, but unsure on how it works

You can use DLookup, or you can open a Recordset to get the data. Use the AfterUPdate event of the textbox:

Sub YourTextbox_AfterUpdate()
Dim rst As DAO.REcordset

Set rst = Currentdb.OpenRecordset("SELECT * FROM YourPersonTable WHERE PersonIDFIeld=" & Me.YourTextbox)
If Not(rst.EOF and rst.BOF) Then
'/now populate the textboxes
Me.txtName = rst("sName")
Me.txtWorkplace = rst("sWorkPlace")
End IF

Set rst = Nothing
End Sub

There are some potential issues with this, however. If the form is bound, then realize that doing this ANYTIME the ID is
updated will also update the underlying table data ... this is probably what you want to do anyway, but just be aware of
what's going on.

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
Scott,
Thanks for the reply, I understand what its doing apart from the lines like
this
Me.txtName = rst("sName")
I am not sure what the ("sName") is to mean?
I have put this into the code and it doesnt seem to work.

Jez
 
Scott's assuming that there's a field sName in the table that contains the
data you want to display in the text box named txtName. If your field name
is something different, replace sName with that field name (still within
quotes).
 
Thanks for this it works, but I have now tried it on another textbox for
using another table and I keep getting a run time error (3075) witha a
message of "Syntax Error (Missing Operator) in querry expression
The error picks up on this line below on

Set rst = Currentdb.OpenRecordset("SELECT * FROM YourPersonTable WHERE
PersonIDFIeld=" & Me.YourTextbox)

It seems to be the section after WHERE. When I have tried the (CTRL+G)
Intermediate window it shows the txtbox with a value which it should and the
place where I want to put in as a null value

Jez
 
Is PersonIDField text? If so, you need quotes around what you're passing as
an argument:

Set rst = Currentdb.OpenRecordset( _
"SELECT * FROM YourPersonTable WHERE " & _
"PersonIDFIeld=" & Chr$(34) & Me.YourTextbox & Chr$(34))

Chr$(34) corresponds to "
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DLookup - why this syntax? 12
dlookup challanges 1
Automatic Dlookup update 5
DLookup in subform 11
DLookup syntax in control on form 7
DLOOKUP CONFUSION 3
DLookUp 4
Dlookup Question 5

Back
Top