SQL recordset syntax problem

Posted on 2004-04-26
Last Modified: 2012-05-04
Hello guys,

I am trying to setup a dynamic menu that gets columns from the db "sections" table and rows from the "pages" table.

I get the following Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[MySQL][ODBC 3.51 Driver][mysqld-4.0.18-nt]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''SELECT * FROM tblPages WHERE SectionID=' & 'rsSections(0)'' at dynmenutest.asp, line 30

There seems to be a problem on the SQL syntax of the rsPages recordset.

Here's the code of my page:

Dim Dbq, Dsn, Dc
Dim sSectionName, sPage, sLink
Dim rsSections
Dim rsSections_numRows

Set rsSections = Server.CreateObject("ADODB.Recordset")
rsSections.ActiveConnection = MM_City_conn_STRING
rsSections.Source = "SELECT * FROM city.sections"
rsSections.CursorType = 0
rsSections.CursorLocation = 2
rsSections.LockType = 1

rsSections_numRows = 0
Dim rsPages
Dim rsPages_numRows

Set rsPages = Server.CreateObject("ADODB.Recordset")
rsPages.ActiveConnection = MM_City_conn_STRING
rsPages.Source = "'SELECT * FROM tblPages WHERE SectionID=' & 'rsSections(0)'"
rsPages.CursorType = 0
rsPages.CursorLocation = 2
rsPages.LockType = 1

rsPages_numRows = 0
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" language="JavaScript1.2" src="stm31.js"></script>

  <script id="Sothink Widgets:dynmenutest.pgt" type="text/javascript" language="JavaScript1.2">
While Not rsSections.EOF
sSectionName = rsSections("Section")

stm_ai("p0i0",[1,"<%=sSectionName%>","","",-1,-1,0,"","_self","","","","",0,0,0,"arrow_r.gif","arrow_r.gif",7,7,0,0,1,"#fffff7",0,"#b5bed6",0,"","",3,3,1,1,"#fffff7","#000000","#000000","#000000","8pt Verdana","8pt Verdana",0,0]);
While Not rsPages.EOF
sPage = rsPages("PageMenuName")
sLink = rsPages("pn")

stm_ai("p1i0",[1,"<%=sPage%>","","",-1,-1,0,"<%=sLink%>","_self","","","","",0,0,0,"","",0,0,0,0,1,"#fffff7",0,"#b5bed6",0,"","",3,3,1,1,"#fffff7","#000000","#000000","#000000","8pt Verdana","8pt Verdana",0,0]);


Question by:matsos
  • 3
  • 2
LVL 19

Expert Comment

ID: 10920703
it looks like you're not concatenating the strings properly for your query

change this line:
rsPages.Source = "'SELECT * FROM tblPages WHERE SectionID=' & 'rsSections(0)'"
rsPages.Source = "SELECT * FROM tblPages WHERE SectionID = " & rsSections(0)

Hope that helps.

Author Comment

ID: 10920810
Thanks dakyd for your comment.

Although it seems that your syntax is correct, it returned the same error again with one difference. It returned the first value of the recordset.

Here it is:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[MySQL][ODBC 3.51 Driver][mysqld-4.0.18-nt]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'City College' at line 1

Accepted Solution

minnirok earned 500 total points
ID: 10921455
You need to enclose the field value in single quotes for the SQL query.  So change it to:

rsPages.Source = "SELECT * FROM tblPages WHERE SectionID = '" & rsSections(0) & "';"

should work.  Note that there are single quotes (') now within your query.  Without these, SQL won't understand that 'City College' is a value (rather than a field or command).
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

LVL 19

Expert Comment

ID: 10921661
minnirok is right, any strings need to wrapped with single quotes or SQL won't realize they are values.  I had assumed that SectionID was an integer, so I left them off.  Hopefully you have what you want now.

Author Comment

ID: 10921737
Thanks for all your replies guys. The page worked fine, no errors, but there still seems to be a problem as the recordset rsPages did not return any values. :-(

Section_ID is an integer though.

Author Comment

ID: 10921881
Ok I figured out what the problem was. The field of the Sections table was not the first field to be retrieved in the Mysql db.
I wish I could give the points to both of you.
Thanks for your help.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Insert Query Help 16 96
select query - oracle 16 92
Working with SharePoint Data 8 46
Problems using Provider=OraOLEDB.Oracle via VBScript/Classic ASP 5 45
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now