Solved DLookup Returning Null (1 Viewer)

LiziM

New member
Local time
Today, 11:23
Joined
Feb 3, 2021
Messages
21
Hi all :) I have worked extensively with Excel VBA, but am having some trouble with VBA in Access, I hope you can help.

A user inputs a product's barcode on a form (frm_scan_IN) then I am using a DLookup to return the category for the product ("ProductCategory"), based on the first few characters from its barcode ("ProductBarcodeIdentifier"), both fields are in the table "tblProducts".

The DLookup returns a Null value, despite there being a record with the specified ProductBarcodeIdentifier. I think it may be an issue relating to data types, however, I have "Dim"ed the variable varCategory and varBarcodeIdentifier as strings, since the fields for Category and BarcodeIdentifier in tbl_Products are of type "short text". I have included my code and a screenshot of the tbl_Products design view so you can see the data types. Let me know if you need more info!

Code:
varBarcodeIdentifier = Left([Forms]![frm_scan_IN]![tb_ProductSerialNumber], Len([Forms]![frm_scan_IN]![tb_ProductSerialNumber]) - 8)
varCategory = DLookup("[ProductCategory]", "tbl_Products", "[ProductBarcodeIdentifier]= 'varBarcodeIdentifier'") 'varCategory=Null, which it shouldn't

1612430459360.png
 

CJ_London

Super Moderator
Staff member
Local time
Today, 10:23
Joined
Feb 19, 2013
Messages
16,619
varCategory = DLookup("[ProductCategory]", "tbl_Products", "[ProductBarcodeIdentifier]= 'varBarcodeIdentifier'")

that is looking for a record which contains 'varBarcodeIdentifier', not it's value

you need to reference the value

varCategory = DLookup("[ProductCategory]", "tbl_Products", "[ProductBarcodeIdentifier]= '" & varBarcodeIdentifier & "'")
 

LiziM

New member
Local time
Today, 11:23
Joined
Feb 3, 2021
Messages
21
that is looking for a record which contains 'varBarcodeIdentifier', not it's value

you need to reference the value

varCategory = DLookup("[ProductCategory]", "tbl_Products", "[ProductBarcodeIdentifier]= '" & varBarcodeIdentifier & "'")
Awesome, working like a dream. Thanks CJ_London, I understand now!
 

Users who are viewing this thread

Top Bottom