Microsoft KB Archive/162977

= ACC97: ASP Query Cannot Be Used with the LIKE Predicate =

Article ID: 162977

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition
 * Microsoft Internet Information Server 3.0
 * Microsoft Internet Information Server 4.0

-



This article was previously published under Q162977



Advanced: Requires expert coding, interoperability, and multiuser skills.



SYMPTOMS
When you browse to an Active Server Pages (ASP) file that was exported from Microsoft Access 97, the Web browser either returns no records or you receive the following error message:

Expression cannot be used with the LIKE predicate in query expression.



CAUSE
Because ASP files use ActiveX Data Objects (ADO) to communicate with ODBC drivers in order to query the back-end data, the SQL statements they contain have different character requirements than typical Microsoft Access SQL statements. ASP files use the percent sign (%) as a wildcard character in SQL statements whereas Microsoft Access uses the asterisk (*).

In addition, when Microsoft Access exports a query that contains a parameter concatenated with a wildcard, the SQL statement that is generated does not contain the correct sequence and number of delimiters around the wildcard.



RESOLUTION
Edit the SQL statements in the ASP files so that they use the percent sign (%) for wildcards.

If parameter queries with wildcards are exported, edit the SQL statements in the ASP file so that they use the percent sign (%) as the wildcard along with the correct sequence and number of delimiters around the wildcard.



MORE INFORMATION
NOTE: This section contains information about editing ASP files, and assumes that you are familiar with editing ASP files. Microsoft Access Product Support professionals do not support customization of any HTML, HTX, IDC, or ASP files.

The following example demonstrates how to change the SQL Statement in an ASP file so that it contains the appropriate wildcard character and the correct sequence and number of delimiters around the wildcard.

 In Microsoft Access 97, open the sample database Northwind.mdb.  Create the following new query called FindName based on the Customers table:

      Query: FindName ---      Type: Select Query

Field: CompanyName Table: Customers Criteria: Like [EnterName] & "*"  On the Query menu, click Parameters.  Type the following in the Query Parameters dialog box, and then click OK:

      Parameter          Data Type [EnterName]       Text </li> Save the FindName query and close it.</li> Select the FindName query in the Database window, and then click Save As/Export on the File menu.</li> In the Save As dialog box, click "To an External File or Database," and then click OK.</li> In the "Save Query 'FindName' In" dialog box, select Microsoft Active Server Pages (*.asp) in the Save As Type box, and type FindName.asp in the File Name box. Note the folder where the files will be exported to. Click Export. The Microsoft Active Server Pages Output Options dialog box appears.</li> In the Data Source Name box, enter the name of a System DSN that points to the sample database Northwind.mdb.

For more information on how to define a system DSN, search the Help index for "ODBC, setting up data sources," and see the following article in the Microsoft Knowledge Base:

159682 "Data Source Name Not Found" Err Msg Opening Web Page</li> In the Server URL box, enter the URL that points to the Web Server location where your ASP files will be stored. For example, if you store the ASP files in the \ASPsamp folder on the \\PubTest server, type http://pubtest/aspsamp/ as your Server URL. Click OK.</li> Click OK in the Enter Parameter Value dialog box that appears. Note that the ASP output creates two files: FindName.HTML and FindName.ASP.</li> Copy FindName.HTML and FindName.ASP to a folder on your Web Server computer where you have both Read and Execute permission. Read permission is necessary to browse the HTML file, and execute permission is necessary to run the ASP file.

For more information about configuring Microsoft Internet Information Server (IIS) permissions, please refer to the IIS Help Index, and see the following article in the Microsoft Knowledge Base:

160754 Error "HTTP/1.0 403 Access Forbidden" Browsing IDC Page</li> Use Notepad or another text editor to open the FindName.ASP file. You need to change the SQL Statement so that it will use the appropriate sequence of parameter and wildcard characters. Change the SQL Statement so that it looks as follows:

sql = "SELECT Customers.CompanyName From Customers WHERE (((Customers.CompanyName) Like '" & Request.QueryString("[EnterName]") & "%'))"

Note that the wildcard is a percent sign (%) sign and the sequence in which the delimiters were concatenated has changed.</li> Save the FindName.ASP file and close it.</li> Start Microsoft Internet Explorer 3.0 or another Web browser program.</li> Type the Uniform Resource Locator (URL) in the address box of your Web browser to view FindName.ASP. For example, if you saved your ASP file and your HTML file in a folder called Test in the wwwroot folder of your Web Server, type:

http://%3Cservername%3E/test/FindName.html

Note that the URL depends upon where your files are located on the Web Server.</li> The FindName.HTML form opens in your Web browser with an [EnterName] box and a Run Query button. Type the letter M in the box, and then click the Run Query button. Note that all records with names that begin with "M" are returned.</li></ol>

<div class="references_section">