Like Statement and string variables

  • Thread starter Thread starter William Roberts
  • Start date Start date
W

William Roberts

Hi,
I am using sql in code behind Access 2000. I want to pass criteria to sql via a string variable. What is the correct syntax for using a string variable with a LIKE Statement? Everything I have tried has failed.

e.g WERE [tableName].[fieldName] NOT LIKE " & "'strVar'" & " "

Kind Thanks

William Roberts
 
I am using sql in code behind Access 2000. I want to pass criteria to sql via a string variable. What is the correct syntax for using a string variable with a LIKE Statement? Everything I have tried has failed.

e.g WERE [tableName].[fieldName] NOT LIKE " & "'strVar'" & " "

Put the quotemarks inside your quoted string and concatenate the value
of strVar; in addition, if you want wildcards to find the value
anywhere within fieldName you must add them too. A LIKE expression
without wildcards will match only the exact value, just the same as
the = operator. Try

WHERE [tableName].[fieldName] NOT LIKE '*" & strVar & "*' "

If strVar contains XYZ, this will be read as

NOT LIKE '*XYZ*'

and will exclude all records where fieldName contains the string XYZ
anywhere within the field.
 
Back
Top