Solved

MSAccess - SQL Statement - Syntax problem

Posted on 2016-07-22
13
30 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 49

Expert Comment

by:Ryan Chong
Comment Utility
>>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
Comment Utility
I am unable to unclick the "Enhanced Protected Mode"
I know the connection to the database is working.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
try make sure the following settings in your IE settings?
tmp.pngtry close all your IE browsers and start to retest
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 142

Expert Comment

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

Author Comment

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

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
THANK YOU!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

7 Experts available now in Live!

Get 1:1 Help Now