'DataType mismatch although data types are same

I have a strange thing happening in my below code.

I get data type of column Sales order Lineitems as well as SO field from form via VarType, both have string data type with number 8, but I still get datatype mismatch error.

Set rs = CurrentDb.OpenRecordset("select * from [Sales Order Lineitems] where [Sales Order Number]=" & CStr(Forms![Sales Order]!SO))

If I run code like this, no error happens

Set rs = CurrentDb.OpenRecordset("select * from [Sales Order Lineitems] where [Sales Order Number]='10007'")

How can I make it dynamic?



Solution 1:[1]

Don't leave out the quotes:

Set rs = CurrentDb.OpenRecordset("select * from [Sales Order Lineitems] where [Sales Order Number]='" & CStr(Forms![Sales Order]!SO) & "'")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gustav