Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

access query to allow apostrophes
Welcome, Guest. Please login or register.
December 03, 2008, 03:28:56 PM
11305 Posts in 1249 Topics by 498 Members
Latest Member: katCheeme
Experts Round Table Network  |  Databases  |  Access  |  access query to allow apostrophes « previous next »
Pages: 1 2 [3] 4
Author Topic: access query to allow apostrophes  (Read 5319 times)
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #30 on: May 02, 2007, 04:02:26 PM »

not SQL
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Johnny26652

Offline Offline

Posts: 61


« Reply #31 on: May 03, 2007, 10:23:29 AM »


<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" -->
Set cmd = Server.CreateObject("ADODB.Command")
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")

conn.open "database connection string"
Set cmd.ActiveConnection = conn


Set param = cmd.CreateParameter("BookName",adVarChar,adParamInput,CLng(40),"bookname")
cmd.Parameters.Append param
cmd.CommandText="insert into tblBook(CatID,BookName,BookTitle) values('"&CatID&"','@Bookname','"&BookTitle&"')"

cmd.Execute, ,adExecuteNoRecords

book name is of type Memo in my data field..

Everything stored perfectly in my database other than the parameter bookname is stored as @Bookname and not the book name i am adding.. seems like i am doing something wrong.. help please..

thanks guys..
« Last Edit: May 03, 2007, 10:25:38 AM by Johnny26652 » Logged

Johnny
nico5038

Offline Offline

Posts: 11


« Reply #32 on: May 03, 2007, 11:11:06 AM »

Try:

values('"&CatID&"','" & @Bookname & "','"&BookTitle&"

When the @Bookname can't hold quotes, else:

values('"&CatID&"'," & chr(34) & @Bookname & chr(34) & "','" & BookTitle & "

Nic;o)
Logged
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #33 on: May 03, 2007, 11:42:50 AM »

No, there should be no quoting of the parameter.

Logged

Rod
Johnny26652

Offline Offline

Posts: 61


« Reply #34 on: May 03, 2007, 11:45:39 AM »

it gives me invalid character when i follow what nico has suggested
Logged

Johnny
nico5038

Offline Offline

Posts: 11


« Reply #35 on: May 03, 2007, 11:57:51 AM »

Sorry Johnny, forgot the trailing ")":

values('"&CatID&"'," & chr(34) & @Bookname & chr(34) & "','" & BookTitle & ")

Nic;o)
Logged
Johnny26652

Offline Offline

Posts: 61


« Reply #36 on: May 03, 2007, 12:46:48 PM »

Nico thanks for your help. It is pointing syntax error at @Bookname. i took care of the trailing.. i am thinking may be i should say param.value = request.form("bookname"). i never used command object so i am not sure..
Logged

Johnny
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #37 on: May 03, 2007, 03:45:49 PM »

nico and johnny do not put single or double quotes around a parameter.

e.g.

cmd.CommandText="insert into tblBook(CatID,BookName,BookTitle) values('"&CatID&"',@Bookname,'"&BookTitle&"')"

The value of @Bookname is set with the
 
Set param = cmd.CreateParameter("BookName",adVarChar,adParamInput,CLng(40),"bookname")
cmd.Parameters.Append param

In your Set.param, the first value is the parameter name; Bookname, not BookName.

The last value of the Set.Param is the value to be assigned to @Bookname, which if posted would be Request.Form("bookname")

Set param = cmd.CreateParameter("@Bookname",adVarChar,adParamInput,CLng(40),Request.Form("bookname"))
cmd.Parameters.Append param

Here is an example from a working query of mine...

"UPDATE regtemp SET posted = 1 WHERE (sessionid=@sessionid) AND (remote_addr=@remote_addr);"

Note both @sessionid and @remote_addr are text fields and there are no quotes at all in the command text.

Now it is odd that you are creating command text with only one parameter rather than three.

You should first have:

cmd.CommandText="insert into tblBook(CatID,BookName,BookTitle) values(@catid,@bookname,@booktitle)"

followed by three

Set param = db_cmd.CreateParameter("@catid",ptype,adParamInput,CLng(Len(CatID)),CatID)
cmd.Parameters.Append param
Set param = db_cmd.CreateParameter("@bookname",ptype,adParamInput,CLng(Len(BookName)),BookName)
cmd.Parameters.Append param
Set param = db_cmd.CreateParameter("@booktitle",ptype,adParamInput,CLng(Len(BookTitle)),BookTitle)
cmd.Parameters.Append param

And in the above you must replace ptype, with the correct type for the corresponding Access column.

For a text column you would have adVarChar, for an integer field adInteger and for an Access 2000/2003 memo field adLongVarWChar.

You do not need to worry if BookName contains quotes or not.  It is not relevant using parameters as the parameter is not executed as SQL.
« Last Edit: May 03, 2007, 06:02:07 PM by rdivilbiss » Logged

Rod
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #38 on: May 03, 2007, 06:00:55 PM »

http://www.rodsdot.com/ee/parameterized_sql_text_parameter.asp

Logged

Rod
Johnny26652

Offline Offline

Posts: 61


« Reply #39 on: May 04, 2007, 06:59:16 AM »

Rod,
thanks for your time. i followed exactly what you said.. now i get this error..


Set param = cmd.CreateParameter("@v_bookname",adLongVarWChar,adParamInput,titlename)
cmd.Parameters.Append param


Provider error '80020005'

Type mismatch.

i think it has to do with memo field.. i am using access 2003


my datafields

id - autonumber
catid - number
bookno - text
bookname - memo
booktitle - memo
addedby - text
« Last Edit: May 04, 2007, 08:24:31 AM by Johnny26652 » Logged

Johnny
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #40 on: May 04, 2007, 10:02:53 AM »

Rod,
thanks for your time. i followed exactly what you said.. now i get this error..


Set param = cmd.CreateParameter("@v_bookname",adLongVarWChar,adParamInput,titlename)
cmd.Parameters.Append param


Provider error '80020005'

Type mismatch.

i think it has to do with memo field.. i am using access 2003



Probably not.  You are missing the size parameter, so the type mismatch is probably where ADO is getting the value of the field when it is expecting the size.

Should be Set param = cmd.CreateParameter("@v_bookname",adLongVarWChar,adParamInput,CLng(Len(titlename)),titlename)

The size has to be a long integer, ergo the CLng, and as long as the length of your memo field is shorter than the maximum length for a memo field in Access, it's safe to just use the exact size of the field value itself.


Logged

Rod
Johnny26652

Offline Offline

Posts: 61


« Reply #41 on: May 04, 2007, 12:57:24 PM »

i just created a table called tblCompany

companyID - Autonumber
companyType - Memo



<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" -->
<%
Dim conn, cmd, rs, command, param, numAffected

Dim v_companytype

v_companytype = "testcompany"

Response.write(v_companytype)

' Note the use of @id, that's the parameter
command = "INSERT INTO tblCompany(companyType) VALUES (@v_companytype)"

Set cmd = Server.CreateObject("ADODB.Command")
Set conn = Server.CreateObject("ADODB.Connection")

conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("testcompany.mdb")
Set cmd.ActiveConnection = conn

' This is where we tell ADODB what our parameter is, it's maximum allowed length
' and it's value.
'
' Doing this means our value is only used as variable and can not
' be executed as in-line SQL
Set param = cmd.CreateParameter("@v_companytype",adLongVarWChar,adParamInput,CLng(Len(v_companytype)),v_companytype)

' We must add our parameter to the command objects Parameters collection
cmd.Parameters.Append param

' We also must assign our SQL command to the ADODB command object.
cmd.CommandText = command

' Now we open a record set using the command object we prepared.
cmd.Execute numAffected,, adExecuteNoRecords

Set param = nothing
Set conn = nothing
Set cmd = nothing
%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>

</body>

</html>


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

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

company.asp, line 34



this is line 34

' Now we open a record set using the command object we prepared.
cmd.Execute numAffected,, adExecuteNoRecords


hopefully i did all the steps right..
« Last Edit: May 04, 2007, 01:08:57 PM by Johnny26652 » Logged

Johnny
rdivilbiss
Governing Council Member
*
Offline Offline

Posts: 414



WWW
« Reply #42 on: May 04, 2007, 02:01:50 PM »

INSERT INTO tblCompany(companyType) VALUES (@v_companytype) needs a space after the table name.
Logged

Rod
Johnny26652

Offline Offline

Posts: 61


« Reply #43 on: May 04, 2007, 02:47:28 PM »

i had a space.. its still giving me the same error.
Logged

Johnny
nico5038

Offline Offline

Posts: 11


« Reply #44 on: May 04, 2007, 03:17:30 PM »

Why not drop the parameter approach and add the value directly by stringing like:

command = "INSERT INTO tblCompany(companyType) VALUES ('" & v_companytype & "')"

or when you want to allow a companytype with a single quote:

command = "INSERT INTO tblCompany(companyType) VALUES (" & chr(34) & v_companytype & chr(34) & ")"

when the type is numeric just use:

command = "INSERT INTO tblCompany(companyType) VALUES (" &  v_companytype  & ")"

Nic;o)

Logged
Pages: 1 2 [3] 4
« previous next »
    Jump to: