Avatar of SteveL13
SteveL13
Flag for United States of America asked on

Trying to hide #Error in a form field

I'm trying to use this code to hide a #Error in a form field but the syntax seems to be wrong.

=IIf(IsNull[txtEquipmentN]),"",DLookUp("[Manufactor/EquipmentType]","tblEquipment","[EquipmentID] = " & [Forms]![frmSites]![txtEquipmentN])

In other words if txtEquipmentN is empty on the form, the field using the DLookup should be blank.  Otherwise use the DLookup.
Microsoft Access

Avatar of undefined
Last Comment
SteveL13

8/22/2022 - Mon
John Tsioumpris

There things to check
1. Correct mapping of your controls to the fields of your Recordsource
2. Your RecordSource so that is not producing #Error...
Paul Cook-Giles

I suspect that you don't have the right number of parenthesis.  I usually test formulae like this in steps:
put in the DLookup, confirm that it's working;  surround it with the IsNull, confirm that it's working, then surround the formula with the IIf.  It's hard to test without having a connection to the data, but try this:

=IIf(isnull([txtEquipmentN]), "", (DLookUp("[Manufactor/EquipmentType]","tblEquipment","[EquipmentID] = " & [Forms]![frmSites]![txtEquipmentN])))
Paul Cook-Giles

If txtEquipmentN is a text field, the Criteria portion of the DLookup needs single quotes around the reference:

DLookUp("[Manufactor/EquipmentType]","tblEquipment","[EquipmentID] = '" & [Forms]![frmSites]![txtEquipmentN] & "'")
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
SteveL13

ASKER
It is a number field but this isn't working:

=IIf(isnull([txtEquipmentN]), "", (DLookUp("[Manufactor/EquipmentType]","tblEquipment","[EquipmentID] = " & [Forms]![frmSites]![txtEquipmentN])))

Am getting, The expression you entered contains invalid syntax.
Paul Cook-Giles

Will the DLookup function work by itself?
SteveL13

ASKER
Yes.  Perfectly.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
SteveL13

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Helen Feddema

I recommend saving all the values from controls to variables of the appropriate data type, so that you can display them for debugging purposes in the Immediate Window.  Then use the variables in your expression, rather than control references.
SteveL13

ASKER
My solution worked perfectly.