Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MSAccess - SQL Statement - Syntax problem

Posted on 2016-07-22
13
Medium Priority
?
37 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 53

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 53

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

721 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