Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MSAccess - SQL Statement - Syntax problem

Posted on 2016-07-22
13
Medium Priority
?
38 Views
Last Modified: 2016-07-23
I created this SQL statement as a query within MSAccess. It generates the exact report I need but when I paste the code into my asp page, I get an ERROR. PAGE CANNOT DISPLAY. Anyone know what the problem is?

<%
'Create recordset
 Dim sql, objRS


 sql = "SELECT gen.City, jls.Title, jls.cost " & _
 "from ( " & _
  	"select ChartID, J1Title, J1Cost from tblJournals union "
 	"select ChartID, J2Title, J2Cost from tblJournals union "
 	"select ChartID, J3Title, J3Cost from tblJournals union "
 	"select ChartID, J4Title, J4Cost from tblJournals union "
 	"select ChartID, J5Title, J5Cost from tblJournals union "
 	"select ChartID, J6Title, J6Cost from tblJournals union "
 	"select ChartID, J7Title, J7Cost from tblJournals union " 
 	"select ChartID, J8Title, J8Cost from tblJournals union "
 	"select ChartID, J9Title, J9Cost from tblJournals union "
 	"select ChartID, J10Title, J10Cost from tblJournals union "
 	"select ChartID, J11Title, J11Cost from tblJournals union "
 	"select ChartID, J12Title, J12Cost from tblJournals union "
 	"select ChartID, J13Title, J13Cost from tblJournals union "
 	"select ChartID, J14Title, J14Cost from tblJournals union "
 	"select ChartID, J15Title, J15Cost from tblJournals union "
 	"select ChartID, J16Title, J16Cost from tblJournals union "
 	"select ChartID, J17Title, J17Cost from tblJournals union "
 	"select ChartID, J18Title, J18Cost from tblJournals union "
 	"select ChartID, J19Title, J19Cost from tblJournals union "
 	"select ChartID, J20Title, J20Cost from tblJournals union "
 	"select ChartID, J21Title, J21Cost from tblJournals union "
 	"select ChartID, J22Title, J22Cost from tblJournals union "
 	"select ChartID, J23Title, J23Cost from tblJournals union "
 	"select ChartID, J24Title, J24Cost from tblJournals union "
 	"select ChartID, J25Title, J25Cost from tblJournals "
 	
 	") jls " & _
	"inner join tblGeneral as gen " & _
	"on gen.ChartID=jls.ChartID "
	
	
 		If Request.querystring("sort") = "" then
 			sql = sql & "ORDER BY Title,City"
 		Else
  			sql = sql & "ORDER BY " & Request.querystring("sort")
  		End If			
	
	Set objRS = Server.CreateObject("ADODB.Recordset")
	objRS.Open sql, objConn


'First, display the TABLE header info:

Response.Write "<br><TABLE class='CCTable' cellspacing='0' width='400px' border='1' cellpadding='5'>"

Response.Write "<TR background-color:white;'>"
Response.Write "<TH class='CCTable' scope='col' width='150px'><b>&nbsp;&nbsp;</b></th>"
Response.Write "<TH class='CCTable' scope='col' width='50px'><b>Title</b></th>"
Response.Write "<TH class='CCTable' scope='col' width='125px'><b>Cost</b></th></tr>"
Response.Write "<TH class='CCTable' scope='col' width='150px'><b>Location</b></th>"

While Not objRS.EOF
counter = counter + 1
	Response.Write "<TR><TD class='GeneralTable' scope='row' width='25px'>" & counter
	Response.Write "<TD class='GeneralTable' scope='row' width='400px'>" & Title & "&nbsp;</TD>" & VbCrLf
	Response.Write "<TD class='GeneralTable' scope='row' width='150px'>$" & Cost & ", " & objRS("State") & "</TD>" & VbCrLf
	Response.Write "<TD class='GeneralTable' scope='row' width='100px'>" & City & "&nbsp;</TD>" & VbCrLf
		
   'Advance the recordset
   objRS.MoveNext

Wend


objRS.Close
Set objRS = Nothing

Response.Write "</TABLE></div>" & vbCrLf


%>

Open in new window

0
Comment
Question by:Malloy1446
  • 6
  • 5
  • 2
13 Comments
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 41725472
>>PAGE CANNOT DISPLAY
try configure your browser so that it shows the exact error out for diagnosis.

"Internet Explorer cannot display the webpage" error
https://support.microsoft.com/en-sg/kb/956196

probably there are some syntax or connection errors within your page.
0
 

Author Comment

by:Malloy1446
ID: 41725485
I am unable to unclick the "Enhanced Protected Mode"
I know the connection to the database is working.
0
 
LVL 54

Expert Comment

by:Ryan Chong
ID: 41725492
try make sure the following settings in your IE settings?
tmp.pngtry close all your IE browsers and start to retest
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41725588
sql = "SELECT gen.City, jls.Title, jls.cost " & _ "from ( " & _       "select ChartID, J1Title, J1Cost from tblJournals union "

you are missing (at least) 1 detail, the "cost" and "title" alias

sql = "SELECT gen.City, jls.Title, jls.cost " & _ "from ( " & _       "select ChartID, J1Title as Title, J1Cost as cost from tblJournals union "
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41725591
besides that, i think your table design is not normalized, and could be largely simplified
instead of having 25*2 fields for title and cost, have them per row, and that would get you rid of this likely slow union query
note: try also union all instead of union ....
0
 

Author Comment

by:Malloy1446
ID: 41725826
I've added UNION ALL to all the select statements.

I changed my IE settings to show HTTP settings. I am getting an "The page cannot be displayed because an internal server error has occurred." error.

sql = "SELECT gen.City, jls.Title as Title, jls.cost as cost " & _
 "from ( " & _
  	"select ChartID, J1Title, J1Cost from tblJournals union all " & _
 	"select ChartID, J2Title, J2Cost from tblJournals union all " & _
 	"select ChartID, J3Title, J3Cost from tblJournals union all " & _
 	"select ChartID, J4Title, J4Cost from tblJournals union all " & _
 	"select ChartID, J5Title, J5Cost from tblJournals union all " & _
 	"select ChartID, J6Title, J6Cost from tblJournals union all " & _
 	"select ChartID, J7Title, J7Cost from tblJournals union all "  & _
 	"select ChartID, J8Title, J8Cost from tblJournals union all " & _
 	"select ChartID, J9Title, J9Cost from tblJournals union all " & _
 	"select ChartID, J10Title, J10Cost from tblJournals union all " & _
 	"select ChartID, J11Title, J11Cost from tblJournals union all " & _
 	"select ChartID, J12Title, J12Cost from tblJournals union all " & _
 	"select ChartID, J13Title, J13Cost from tblJournals union all " & _
 	"select ChartID, J14Title, J14Cost from tblJournals union all " & _
 	"select ChartID, J15Title, J15Cost from tblJournals union all " & _
 	"select ChartID, J16Title, J16Cost from tblJournals union all " & _
 	"select ChartID, J17Title, J17Cost from tblJournals union all " & _
 	"select ChartID, J18Title, J18Cost from tblJournals union all " & _
 	"select ChartID, J19Title, J19Cost from tblJournals union all " & _
 	"select ChartID, J20Title, J20Cost from tblJournals union all " & _
 	"select ChartID, J21Title, J21Cost from tblJournals union all " & _
 	"select ChartID, J22Title, J22Cost from tblJournals union all " & _
 	"select ChartID, J23Title, J23Cost from tblJournals union all " & _
 	"select ChartID, J24Title, J24Cost from tblJournals union all " & _
 	"select ChartID, J25Title, J25Cost from tblJournals "
 	
 	") jls " & _
	"inner join tblGeneral as gen " & _
	"on gen.ChartID=jls.ChartID "
	

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41725829
you did not apply the other fix yet...
0
 

Author Comment

by:Malloy1446
ID: 41725847
Sorry Guy, I am at the learning stage using UNION ALL. I am unsure of what the other fix is.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41725852
please see my first post above

the uinion all is not an error fix, but a performance improvement suggestion

if the fix does not solve the error, you need to implement proper error handling so you can produce a proper error message:
https://support.microsoft.com/en-us/kb/300043
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41725853
please see my first post above

the uinion all is not an error fix, but a performance improvement suggestion

if the fix does not solve the error, you need to implement proper error handling so you can produce a proper error message:
https://support.microsoft.com/en-us/kb/300043
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 41725854
please see my first post above

the uinion all is not an error fix, but a performance improvement suggestion

if the fix does not solve the error, you need to implement proper error handling so you can produce a proper error message:
https://support.microsoft.com/en-us/kb/300043
0
 

Author Comment

by:Malloy1446
ID: 41725914
SUCCESS!!

Had to add a WHERE clause to the SQL statement to eliminate CITY records where there was no Journal date.

THank you for your patience!
0
 

Author Closing Comment

by:Malloy1446
ID: 41725915
THANK YOU!!!
0

Featured Post

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.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

963 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