DLookUp on Update Query (1 Viewer)

fedebrin

Registered User.
Local time
Today, 07:05
Joined
Sep 20, 2017
Messages
59
Dear all,

Trying to use a DLookUp formula in an update query to clean up a code
There are 2 tables with the following:

  • MainTable which Contains the fields: MainDirtyCode and MainCleanedCode
  • CodeCleanUp which Contains the fields: DirtyCode and CleanedCode

Update Query to modify MainCleanedCode:

DLookUp("CleanedCode","CodeCleanUp","DirtyCode"="MainDirtyCode")

The query runs but I am not getting anything updated.
Your help is greatly appreciated.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:05
Joined
Aug 30, 2003
Messages
36,125
What's the SQL of the query? I'd expect to be able to join those tables in the query rather than use DLookup().
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 10:05
Joined
May 21, 2018
Messages
8,527
agree with pbaldy. However the way you wrote that
Code:
"DirtyCode"="M ainDirtyCode"
it is going to look for DirtyCode that exactly equals the string "M ainDirtyCode"
It would be more like
Code:
"DirtyCode = '" & [MainDirtyCode] & "'"
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:05
Joined
Feb 19, 2002
Messages
43,257
DLookup() is an inefficient method. Just join the two tables on the "dirty" value and update the "clean" value.

DLookup() runs a separate query for each row and so cannot be optimized. If you have a thousand rows to update, A thousand queries will have to run. The join is a more efficient operation and lets the query engine pass the data only once.
 

fedebrin

Registered User.
Local time
Today, 07:05
Joined
Sep 20, 2017
Messages
59
Thank you all for your responses.

How would I go about a Union SQL? I am not familiar unfortunately with this language.

Thanks in advance,
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:05
Joined
Aug 30, 2003
Messages
36,125
In general terms it would look like:

UPDATE TargetTable
INNER JOIN SourceTable ON TargetTable.KeyField = SourceTable.KeyField
SET TargetTable.FieldName = SourceTable.FieldName
 

fedebrin

Registered User.
Local time
Today, 07:05
Joined
Sep 20, 2017
Messages
59
Thank you!

One thing I don't understand... you do not need to do the DlookUp and this Union query on the same QUery right?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 07:05
Joined
Aug 30, 2003
Messages
36,125
For clarity, this is not a union query, this is an update query. No, you do not need the DLookup() with this. The join between the tables enables you to get the values from one table to put in the other.
 

Users who are viewing this thread

Top Bottom