Access Form Combobox Partial Filter

2019-08-20 06:23发布

问题:

I'm trying to write a partial text match, to filter a form, from a combobox.

This is what I was working on writing:

"[FieldName1] Like '*" & Replace(Me.cboFindRecord.Text, "'", """) & "*' OR [FieldName2] Like '*" & Replace(Me.cboFindRecord.Text, "'", """) & "*'"

When I leave that line of the VBA code, I get an error at the 2nd & "*', highlighting the single quote ', and it says Compile error: Expected expression.

Anyone know what I'm doing wrong? I've checked the quotes and double quotes again and again.

回答1:

, """)

is not a valid string.

If you want to use a double quote character, it would be

"[FieldName1] Like '*" & Replace(Me.cboFindRecord.Text, "'", """") & "*' OR ..."

(an escaped double quote inside a string)

But the usual way of escaping single quotes is to use two single quotes:

"[FieldName1] Like '*" & Replace(Me.cboFindRecord.Text, "'", "''") & "*' OR ..."