Print Page | Close Window

Data type mismatch in criteria expression

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=5960
Printed Date: 31 March 2026 at 1:17pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Data type mismatch in criteria expression
Posted By: twooly
Subject: Data type mismatch in criteria expression
Date Posted: 24 September 2003 at 6:02pm

I am trying to create a script to view/edit a profile.  I can login and pass session variables between pages but I am getting this error

[Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

/viewprofile.asp, line 54

If I comment things out I can see the SQL string and that is correct.  I can also see the userID variable too just fine.

PS line 54 is "rsViewProfile.Open strSQL, adoCon"

 

Here is the code

<%
'Dimension Variables
Dim strUserName 'Holds the name of the user
Dim userID
Dim adoCon         'Holds the Database Connection Object
Dim rsViewProfile   'Holds the recordset for the records in the database
Dim strSQL          'Holds the SQL query to query the database

'Get the users name passed from the previous page
strUserName = Session("userName")

userID = Session("userID")


'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("toddwoolums_site.mdb")

'Create an ADO recordset object
Set rsViewProfile = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT firstname FROM tblUsers WHERE ID ='" & userID & "'"

'Open the recordset with the SQL query
rsViewProfile.Open strSQL, adoCon


If NOT rsViewProfile.EOF Then

 Response.Write ("<br>")
     Response.Write (rsViewProfile("firstname"))
 
     Response.Write ("<br>")
    
     Response.Write ("<br>")
End If

'Reset server objects
rsViewProfile.Close
Set rsViewProfile = Nothing
Set adoCon = Nothing
%>

sql string <% = strSQL %>
 Your user id is <% =userID %>




Replies:
Posted By: Gullanian
Date Posted: 24 September 2003 at 6:09pm

Try changing:

strSQL = "SELECT firstname FROM tblUsers WHERE ID ='" & userID & "'"

To

strSQL = "SELECT firstname FROM tblUsers WHERE ID =" & userID



Posted By: twooly
Date Posted: 24 September 2003 at 9:21pm

Now I get this

 

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

[Microsoft][ODBC Microsoft Access Driver] Syntax error in string in query expression 'ID ='1'.

/viewprofile.asp, line 54



Posted By: twooly
Date Posted: 24 September 2003 at 9:22pm
Never mind spoke too soon.  Works now


Posted By: hassack
Date Posted: 23 October 2003 at 12:51pm
How twooly??? i have the same problem..!!!!


Posted By: KCWebMonkey
Date Posted: 23 October 2003 at 1:00pm
umm, then do the same solution...


Posted By: hassack
Date Posted: 23 October 2003 at 4:51pm
I think may be he has another solution ... , i have the same second problem when used the first example, but no problem i can fix right now


Posted By: twooly
Date Posted: 24 October 2003 at 9:01am

Just use the sql string given above

strSQL = "SELECT firstname FROM tblUsers WHERE ID =" & userID



Posted By: MorningZ
Date Posted: 24 October 2003 at 10:14am

i can tell by this thread exactly what the second problem

Gullian posted the correct code:
strSQL = "SELECT firstname FROM tblUsers WHERE ID ='" & userID & "'"
(which btw is trying to compare the integer field "ID" to the string "userID", hence the data doesn't match in type, hence the "data mismatch" error)

To
strSQL = "SELECT firstname FROM tblUsers WHERE ID =" & userID

twooly just went and hacked off the last concatination of the string, so he went from:
strSQL = "SELECT firstname FROM tblUsers WHERE ID ='" & userID & "'"
To
strSQL = "SELECT firstname FROM tblUsers WHERE ID ='" & userID

notice the bold tic mark still left, so when he went to run the page, ADO saw:
SELECT firstname FROM tblUsers WHERE ID ='1

and coughed up the proverbial hairball......

classic case of "tell me how to fix it" without understanding why it broke



-------------
Contribute to the working anarchy we fondly call the Internet



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net