简体   繁体   中英

issue with DLOOKUP syntax error for new database

I have a database with 4 tables but primarily it is a diversion table/form (DiversionT/F) and a payback table/form (PaybackT/F). Basically, when my program loans parts to other programs in my organization a diversion is created in DiversionT. When the program want to payback they create a payback entry in PaybackT.

I have an issue that I am confused about: On PaybackF the user enters an NSN (long part code) that they want to payback. Ultimately, I want some of the form to auto populate with part info based on the NSN entered. The info is stored in DiversionT. I have created a few text boxes on PaybackF to show the info. The first text box I am trying to autofill based on the NSN is a textbox called PartName. It should search DiversionT for that NSN and fill in the appropriate PartName on PaybackF. In the control Source for the box I typed:

=DLookUp("[PartName]","[DiversionT]", "[PartName]=" & Forms![PaybackF]!NSN)

I get the following error:

The expression you entered contains invalid syntax.

To be perfectly honest I don't really understand VBA yet (spent my life until now with C, C++, Java, and Python) but looked up the function on the Microsoft site.

If I am not going about this right, please also let me know?

You can't enter VBA in to form properties like source control. VBA is only used in procedural code in the VBE. That being said, there is a DLOOKUP function available to form fields and the syntax is similar. This is a cause of your confusion.

EXAMPLE SYNTAX FOR NUMBERS:

=DLookUp("[PartName]","DiversionT", "[PartName]=" & [NSN])

EXAMPLE SYNTAX FOR STRINGS:

=DLookUp("[PartName]","DiversionT", "[PartName]='" & [NSN] & "'")

NOTE: I can't tell what form you are on or if NSN is from a parent form or subform. Basically, NSN needs to be readable from the same form where that text field exists.

When using DLookup to get data, if you are dealing with text strings, you need to ensure that you use single quotes to wrap the text in.

Also, I think that your logic in the DLookup is slightly wrong. I think that this is what you are after:

=DLookUp("[PartName]","[DiversionT]", "[NSN]='" & Me!NSN & "'")

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM