473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error in dynamic sql: Data type mismatch in criteria

Hi,
I am trying to test a sql statement in Access which gives me
the error as stated in the heading.
The sql statement is built as a part of asp login verification,
where the userid and password are input in login screen.
The password in the database is a number field.

I am writing the dynamic sql statement as follows below. I believe
I am going wrong in the password section of the code. I
appreciate any help. Thanks. Regards.
Set CN=server.creat eobject("ADODB. Connection")
CN.Open myDSN

Set RS = Server.CreateOb ject("ADODB.Rec ordset")
RS.ActiveConnec tion=CN

strSQL = "select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password from
qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND " & _
"qrySubGrantCom bo.Password ='" & Request.Form("t xt_Password") & "'"
Response.Write strSQL

Jul 22 '05 #1
7 3415
What do you get when you write out the sql statement?

--
Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com
"Jack" <Ja**@discussio ns.microsoft.co m> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
Hi,
I am trying to test a sql statement in Access which gives me
the error as stated in the heading.
The sql statement is built as a part of asp login verification,
where the userid and password are input in login screen.
The password in the database is a number field.

I am writing the dynamic sql statement as follows below. I believe
I am going wrong in the password section of the code. I
appreciate any help. Thanks. Regards.
Set CN=server.creat eobject("ADODB. Connection")
CN.Open myDSN

Set RS = Server.CreateOb ject("ADODB.Rec ordset")
RS.ActiveConnec tion=CN

strSQL = "select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password from qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND " & _ "qrySubGrantCom bo.Password ='" & Request.Form("t xt_Password") & "'"
Response.Write strSQL

Jul 22 '05 #2
"Jack" <Ja**@discussio ns.microsoft.co m> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
Hi,
I am trying to test a sql statement in Access which gives me
the error as stated in the heading.
The sql statement is built as a part of asp login verification,
where the userid and password are input in login screen.
The password in the database is a number field.

I am writing the dynamic sql statement as follows below. I believe
I am going wrong in the password section of the code. I
appreciate any help. Thanks. Regards.
Set CN=server.creat eobject("ADODB. Connection")
CN.Open myDSN

Set RS = Server.CreateOb ject("ADODB.Rec ordset")
RS.ActiveConnec tion=CN

strSQL = "select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password
from
qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND " &
_
"qrySubGrantCom bo.Password ='" & Request.Form("t xt_Password") & "'"
Response.Write strSQL

Here are the rules for delimiting data in dynamic sql strings, particularly
in the WHERE clause:
To decide whether or not to delimit the data, look at the datatype of the
FIELD BEING COMPARED TO - NOT THE DATA.

1. If it's a numeric field, you must supply it with numeric data, which
means you MUST NOT delimit the data by putting quotes around it.

2. If it's a character/text field, then you must supply string data by
delimiting the data either with single or double quotes. If the data
contains literal quotes, you must escape them by doubling them. This means
that if you use single quotes (apostrophes) for your string delimiters, and
the data contains an apostrophe, then you must replace the apostrophe with
two apostrophes, like this:
Update tbl set textfield = 'O''Malley'
In Access, you can use double quotes for your delimiters, so this will work
as well:
Update tbl set textfield = "O'Malley"
Note: you don't have to escape the apostrophe in O'Malley when you use
double quotes as the delimiter. However, you will need to escape the double
quotes when assigning this statement to a variable:
sSQL = "Update tbl set textfield = ""O'Malley" ""
So most people will use the single quotes and escape the apostrophe:
sName = "O'Malley"
sSQL = "Update tbl set textfield = '" & Replace(sName," '","''") & "'"
response.write sSQL

3. If it's a date/Time field, then the delimiters depend on the type of
database. Since you are using Access, then you must delimit the data with
hash marks (#). Additionally, you must supply the data in US format
(m/d/yyyy) or in ISO (yyyy-mm-dd), with the latter being the more
recommended.

4.Lastly, if you are using LIKE, you need to be aware that you must use %
and _ as the wildcards, not * and ?. This is true no matter what database
you are using

And then, when you think you have it right and it still does not work,
response.write it to see the result of your concatenation. If you've done it
correctly, you will have a statement that you can copy and paste from the
browser window into the SQL View of an Access Query Builder and run without
modification (unless you need to replace the wildcards with the Jet
wildcards).

This all seems rather difficult, doesn't it? Add to this the fact that a
dynamic sql query will not perform as well as a saved query/stored
procedure, and you have two strikes against it. Add the lack of security due
to leaving yourself open to a SQL Injection attack and you have three
strikes. Why did you say you wanted to do it this way ...?

Let me show you how easy this can be using a saved parameter query. let's go
back to your statement and parameterize it* :

UPDATE tblListingspric eChanges SET NewPrice = [P1],
ChangeDate = [P2], [Name]=[P3], Original_Price=[P4]
WHERE PriceChangeID = [P5]

Do you notice ANY delimiters in the above sql statement? :-)
Test this statement in the Access Query Builder by running it: you will be
prompted to supply values for each of the parameters. Supply some values and
make sure it works as intended. When you've finished debugging it, save it
as qUpdPriceChange . Notice that you've created and debugged your query in
the environment where debugging and testing of queries should be done: in
the database environment.

Now to run it in ASP:
'create and open a connection object, cn, populate and
'validate your data variables, then:
cn.qUpdPriceCha nge NewPrice, ChangeDate, Name, _
Original_Price, PriceChangeI
If you are running a query that returns records, you can still use this
syntax, by supplying a recordset variable as an extra argument:

set rs = server.createob ject("adodb.rec ordset")
cn.QueryName parm1,...parmN, rs
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I don't
check it very often. If you must reply off-line, then remove the "NO SPAM"
Jul 22 '05 #3
Mark,
This is the following sql statement I am getting with a sample userid and
password:
select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password from
qrySubGrantComb o where qrySubGrantComb o.ComboID ='00-H15-81366' AND
qrySubGrantComb o.Password ='81366'
Here in the final output, if I do not have the ' sign on both sides of 81366
then, the query result is fine and the query does not give error as: Data
type mismatch in criteria expression. Thanks.

"Mark Schupp" wrote:
What do you get when you write out the sql statement?

--
Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com
"Jack" <Ja**@discussio ns.microsoft.co m> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
Hi,
I am trying to test a sql statement in Access which gives me
the error as stated in the heading.
The sql statement is built as a part of asp login verification,
where the userid and password are input in login screen.
The password in the database is a number field.

I am writing the dynamic sql statement as follows below. I believe
I am going wrong in the password section of the code. I
appreciate any help. Thanks. Regards.
Set CN=server.creat eobject("ADODB. Connection")
CN.Open myDSN

Set RS = Server.CreateOb ject("ADODB.Rec ordset")
RS.ActiveConnec tion=CN

strSQL = "select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password

from
qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND " &

_
"qrySubGrantCom bo.Password ='" & Request.Form("t xt_Password") & "'"
Response.Write strSQL


Jul 22 '05 #4
Thanks for your advise Bob. I appreciate it. As per your advise, I just took
out the quotes for numeric value. Now I have
strSQL = "select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password from
qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND " & _
"qrySubGrantCom bo.Password = & Request.Form("t xt_Password") & "

However, with the change it is generating the following error:
Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/gwisnewcon/verify.asp, line 25, column 46
"qrySubGrantCom bo.Password = & Request.Form("t xt_Password") & "

---------------------------------------------^

Where am I going wrong?

"Bob Barrows [MVP]" wrote:
"Jack" <Ja**@discussio ns.microsoft.co m> wrote in message
news:FB******** *************** ***********@mic rosoft.com...
Hi,
I am trying to test a sql statement in Access which gives me
the error as stated in the heading.
The sql statement is built as a part of asp login verification,
where the userid and password are input in login screen.
The password in the database is a number field.

I am writing the dynamic sql statement as follows below. I believe
I am going wrong in the password section of the code. I
appreciate any help. Thanks. Regards.
Set CN=server.creat eobject("ADODB. Connection")
CN.Open myDSN

Set RS = Server.CreateOb ject("ADODB.Rec ordset")
RS.ActiveConnec tion=CN

strSQL = "select qrySubGrantComb o.ComboID, qrySubGrantComb o.Password
from
qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND " &
_
"qrySubGrantCom bo.Password ='" & Request.Form("t xt_Password") & "'"
Response.Write strSQL

Here are the rules for delimiting data in dynamic sql strings, particularly
in the WHERE clause:
To decide whether or not to delimit the data, look at the datatype of the
FIELD BEING COMPARED TO - NOT THE DATA.

1. If it's a numeric field, you must supply it with numeric data, which
means you MUST NOT delimit the data by putting quotes around it.

2. If it's a character/text field, then you must supply string data by
delimiting the data either with single or double quotes. If the data
contains literal quotes, you must escape them by doubling them. This means
that if you use single quotes (apostrophes) for your string delimiters, and
the data contains an apostrophe, then you must replace the apostrophe with
two apostrophes, like this:
Update tbl set textfield = 'O''Malley'
In Access, you can use double quotes for your delimiters, so this will work
as well:
Update tbl set textfield = "O'Malley"
Note: you don't have to escape the apostrophe in O'Malley when you use
double quotes as the delimiter. However, you will need to escape the double
quotes when assigning this statement to a variable:
sSQL = "Update tbl set textfield = ""O'Malley" ""
So most people will use the single quotes and escape the apostrophe:
sName = "O'Malley"
sSQL = "Update tbl set textfield = '" & Replace(sName," '","''") & "'"
response.write sSQL

3. If it's a date/Time field, then the delimiters depend on the type of
database. Since you are using Access, then you must delimit the data with
hash marks (#). Additionally, you must supply the data in US format
(m/d/yyyy) or in ISO (yyyy-mm-dd), with the latter being the more
recommended.

4.Lastly, if you are using LIKE, you need to be aware that you must use %
and _ as the wildcards, not * and ?. This is true no matter what database
you are using

And then, when you think you have it right and it still does not work,
response.write it to see the result of your concatenation. If you've done it
correctly, you will have a statement that you can copy and paste from the
browser window into the SQL View of an Access Query Builder and run without
modification (unless you need to replace the wildcards with the Jet
wildcards).

This all seems rather difficult, doesn't it? Add to this the fact that a
dynamic sql query will not perform as well as a saved query/stored
procedure, and you have two strikes against it. Add the lack of security due
to leaving yourself open to a SQL Injection attack and you have three
strikes. Why did you say you wanted to do it this way ...?

Let me show you how easy this can be using a saved parameter query. let's go
back to your statement and parameterize it* :

UPDATE tblListingspric eChanges SET NewPrice = [P1],
ChangeDate = [P2], [Name]=[P3], Original_Price=[P4]
WHERE PriceChangeID = [P5]

Do you notice ANY delimiters in the above sql statement? :-)
Test this statement in the Access Query Builder by running it: you will be
prompted to supply values for each of the parameters. Supply some values and
make sure it works as intended. When you've finished debugging it, save it
as qUpdPriceChange . Notice that you've created and debugged your query in
the environment where debugging and testing of queries should be done: in
the database environment.

Now to run it in ASP:
'create and open a connection object, cn, populate and
'validate your data variables, then:
cn.qUpdPriceCha nge NewPrice, ChangeDate, Name, _
Original_Price, PriceChangeI
If you are running a query that returns records, you can still use this
syntax, by supplying a recordset variable as an extra argument:

set rs = server.createob ject("adodb.rec ordset")
cn.QueryName parm1,...parmN, rs
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I don't
check it very often. If you must reply off-line, then remove the "NO SPAM"

Jul 22 '05 #5
Jack wrote:
Thanks for your advise Bob. I appreciate it. As per your advise, I
just took out the quotes for numeric value. Now I have
strSQL = "select qrySubGrantComb o.ComboID,
qrySubGrantComb o.Password from qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND
" & _ "qrySubGrantCom bo.Password = & Request.Form("t xt_Password") &
"

However, with the change it is generating the following error:
Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/gwisnewcon/verify.asp, line 25, column 46
"qrySubGrantCom bo.Password = & Request.Form("t xt_Password") & "

---------------------------------------------^

Where am I going wrong?

You're not showing us the result of

response.write strSQL

You/we cannot troubleshoot a sql statement without knowing what it is.
Showing us the vbscript code that is supposed to generate a sql statement is
not enough. I think I mention that in my previous post.

Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #6
Jack wrote:
Thanks for your advise Bob. I appreciate it. As per your advise, I
just took out the quotes for numeric value. Now I have


You can make this string shorter and more readable by not qualifying the
column names with the table/query name. There's only one table/query in the
FROM clause so there is no chance of confusion.
strSQL = "select qrySubGrantComb o.ComboID,
qrySubGrantComb o.Password from qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND
" & _ "qrySubGrantCom bo.Password = & Request.Form("t xt_Password") &
"


Rewritten, it looks like:
strSQL = "select ComboID,Passwor d " & _
"from qrySubGrantComb o where " & _
"ComboID ='" & Request.Form("t xt_UserName") & "' AND " & _
"Password = & Request.Form("t xt_Password") & "

Do you see the problem? Concentrate on the 4th line.

What do you need to do before concatenating a new string to an existing
string? Answer: complete the existing string.

"Password =

is not complete until you close/delimit it with an ending quote.

"Password = " & Request.Form("t xt_Password")

And then, you attempt to concatenate the beginning of a string ... What is
the purpose of that final & "?

Again, you can avoid this delimiter nonsense by using saved parameter
queries as demonstrated in my initial reply.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #7
Thanks for the feedback again. You guys are just great. Regards.

"Bob Barrows [MVP]" wrote:
Jack wrote:
Thanks for your advise Bob. I appreciate it. As per your advise, I
just took out the quotes for numeric value. Now I have


You can make this string shorter and more readable by not qualifying the
column names with the table/query name. There's only one table/query in the
FROM clause so there is no chance of confusion.
strSQL = "select qrySubGrantComb o.ComboID,
qrySubGrantComb o.Password from qrySubGrantComb o where " & _
"qrySubGrantCom bo.ComboID ='" & Request.Form("t xt_UserName") & "' AND
" & _ "qrySubGrantCom bo.Password = & Request.Form("t xt_Password") &
"


Rewritten, it looks like:
strSQL = "select ComboID,Passwor d " & _
"from qrySubGrantComb o where " & _
"ComboID ='" & Request.Form("t xt_UserName") & "' AND " & _
"Password = & Request.Form("t xt_Password") & "

Do you see the problem? Concentrate on the 4th line.

What do you need to do before concatenating a new string to an existing
string? Answer: complete the existing string.

"Password =

is not complete until you close/delimit it with an ending quote.

"Password = " & Request.Form("t xt_Password")

And then, you attempt to concatenate the beginning of a string ... What is
the purpose of that final & "?

Again, you can avoid this delimiter nonsense by using saved parameter
queries as demonstrated in my initial reply.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10489
by: John Davis | last post by:
I don't understand when I do (2), it will throw the error when I run the ASP. (1) is working fine. Should I use a single quote between the string in SQL statement in ASP? (1) strSQL = "SELECT * FROM DATA WHERE id = " & strParm1 (2) strSQL = "SELECT * FROM DATA WHERE id = " & "'" & strParm1 & "'" Error Type: Microsoft JET Database Engine (0x80040E07) Data type mismatch in criteria expression.
4
11967
by: Mike | last post by:
I am getting a type mismatch error when I do a bulk insert. ---Begin Error Msg--- Server: Msg 4864, Level 16, State 1, Line 1 Bulk insert data conversion error (type mismatch) for row 1, column 14 (STDCOST). ---End Error Msg--- The STDCOST is set to decimal (28,14) and is a formatted in Access as a number, single with 14 decimal. I don't know why I would be getting a Type
3
3977
by: Laurel | last post by:
this is driving me crazy. i need to use a form control as a criteria in a select query, and the control's value is set depending upon what a user selects in an option group on the form. the query results should return information on either a single employee or all employees. the problem that i have is that if i type in "*" (but without quotes) in the QBE criteria, it works fine. however, if i use Iif() to determine whether or not there...
2
10818
by: Chicken Kebab Abdullah | last post by:
Does anyone know why I get the error 3464 Data type mismatch from the following code. I have a form with a combo(to choose a consumable) and 2 list boxes on it. list on left is all printers and list on right shows the ones currently compatible with the consumable chosen in the combo at the top.
0
2253
by: news.paradise.net.nz | last post by:
I have been developing access databases for over 5 years. I have a large database and I have struck this problem with it before but can find nothing in help or online. Access 2000 I have a query that will run fine without any criteria but as soon as I add any criteria it gives a "Data type mismatch" error. As soon as I remove any criteria it runs perfectly. I know this query is based on another query but I have other processes based on...
1
6544
by: ArcadeJr | last post by:
Good morning all! I have been getting a Run-time Error message #3464 - Data Type mismatch in criteria expression. While trying to run a query. I have a database where the field Asset_Number was once a type Number, but I had to change it to a type Text due to I needed to have two zeros at the beginning of the Asset Number (EX: 001234.) The rease I am writing is that now when I run from My Query Menu a
3
6509
by: martlaco1 | last post by:
Trying to fix a query that (I thought) had worked once upon a time, and I keep getting a Data Type Mismatch error whenever I enter any criteria for an expression using a Mid function. Without the criteria, the Mid function returns the values when I run the query. So if one of the values is a "t" (no quotes), can I not ask to isolate that record by putting "t" as a criteria? Nope - error, error. If I put it within the expression itself...
5
2227
by: David | last post by:
Hi, Getting the following error:- Microsoft JET Database Engine error '80040e07' Data type mismatch in criteria expression. The code is as follows, and the last line is the one that produces the error.
6
2099
by: BaWork | last post by:
I have the following to insert a new record: <% .. Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "DSN=qqqqq;" SQLStmt = "INSERT INTO tbl_qqqqqq (main_cat, cat_fee, fee_amount) " SQLStmt = SQLStmt & "VALUES ('" & main_cat & "','" & cat_fee & "','" & fee_amount & "')"
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.