Help with subtracting two dlookup fields (1 Viewer)

gojets1721

Registered User.
Local time
Today, 03:08
Joined
Jun 11, 2019
Messages
430
On a report, I have two fields using dlookups and they both show percentages.

I have a third field that is meant to calculate the difference between the two. I subtracted one field's name from another but am getting a #Type error.

Any suggestions on how to correctly do this?
 

GPGeorge

Grover Park George
Local time
Today, 03:08
Joined
Nov 25, 2004
Messages
1,882
Lookup Fields in a table? Ouch. That's the hard road for sure. Most experienced Access developers would tell you that lookups, or combo boxes, are interface features, and interface features belong in interface objects, i.e in forms. Microsoft fell for the trap, and put them into Access tables, but that has almost always led to woe and sorrow for those who use them,

So, that's the big problem. However, we also need a bit more detail to help you solve this dilemma.

Where is the third field supposed to go? In the same table? In a report? In a form?
 

gojets1721

Registered User.
Local time
Today, 03:08
Joined
Jun 11, 2019
Messages
430
Lookup Fields in a table? Ouch. That's the hard road for sure. Most experienced Access developers would tell you that lookups, or combo boxes, are interface features, and interface features belong in interface objects, i.e in forms. Microsoft fell for the trap, and put them into Access tables, but that has almost always led to woe and sorrow for those who use them,

So, that's the big problem. However, we also need a bit more detail to help you solve this dilemma.

Where is the third field supposed to go? In the same table? In a report? In a form?
So the report has three fields right next to one another. First two are lookups to tables and the third is meant to show the difference between the two fields
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:08
Joined
May 7, 2009
Messages
19,247
use same Expression you used on the two textbox for your third textbox:

'ex:
'[dlookup of first textbox] - [dlookup of second textbox]

=Nz(Dlookup("field1","table1"),0) - Nz(DLookup("field2","table2"), 0)
 

GPGeorge

Grover Park George
Local time
Today, 03:08
Joined
Nov 25, 2004
Messages
1,882
So the report has three fields right next to one another. First two are lookups to tables and the third is meant to show the difference between the two fields
You probably need to use the VALUES from the lookups on the report, not the BOUND columns.
 

June7

AWF VIP
Local time
Today, 02:08
Joined
Mar 9, 2014
Messages
5,479
By "two fields using dlookups" do you mean textboxes? What are the textbox names? Shouldn't have to repeat expressions. Works for me.
Provide sample data and the expressions used. Could provide db for analysis.
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:08
Joined
Jan 20, 2009
Messages
12,853
If both DLookups are from the same table you would only need one DLookup. The first parameter of Domain Functions is an expression, not a fieldname.

Code:
DLookup("field1 - field2","tablename", {condition})
 

KitaYama

Well-known member
Local time
Today, 19:08
Joined
Jan 6, 2022
Messages
1,546
If both DLookups are from the same table you would only need one DLookup. The first parameter of Domain Functions is an expression, not a fieldname.

Code:
DLookup("field1 - field2","tablename", {condition})
Didn't know I can do math in one lookup.
Thanks...
 
Last edited:

June7

AWF VIP
Local time
Today, 02:08
Joined
Mar 9, 2014
Messages
5,479
Good point, Galaxiom.

Also, can either field be Null? Arithmetic with Null returns Null.

Without any criteria, DLookup will return value from first record it encounters so could get unexpected results.
 

June7

AWF VIP
Local time
Today, 02:08
Joined
Mar 9, 2014
Messages
5,479
If you want don't want null to return then handle with Nz()
"Nz(field1) - Nz(field2)"
 

gojets1721

Registered User.
Local time
Today, 03:08
Joined
Jun 11, 2019
Messages
430
By "two fields using dlookups" do you mean textboxes? What are the textbox names? Shouldn't have to repeat expressions. Works for me.
Provide sample data and the expressions used. Could provide db for analysis.
@June7 See the (extremely simplified) attached example DB.
 

Attachments

  • Example22.accdb
    496 KB · Views: 83

June7

AWF VIP
Local time
Today, 02:08
Joined
Mar 9, 2014
Messages
5,479
Your data is text (why?), not number, including a % symbol. Hence the #Type error with arithmetic. Without % symbol, arithmetic would work. Otherwise, have to handle it:

=Val([txtEmployeeScore])-Val([txtBenchmark])

These tables do not have a relationship. tblAudit is not normalized. Audit1 should not be a field. Normalized structure would be fields:

EmployeeID, AuditID, Score.

Then you could do a query that joins tables on AuditID and avoid slow performance introduced by domain aggregate functions.
 
Last edited:

Users who are viewing this thread

Top Bottom