Classic ASP: Trouble processing SQL

141 views Asked by At

I'm trying to develop a query builder for non-technical folk, however I am having trouble creating a custom query string for the database to process. Here is my code:

<%
'Declare variables
dim query, myDSN, Connect, rs
dim formdate1, formdate2
dim from, sel

'Provider String and DB location
myDSN = "PROVIDER=MICROSOFT.ACE.OLEDB.12.0;DATA SOURCE=" & Server.Mappath("/MasterDB.accdb")
'myDSN = "PROVIDER=SQLOLEDB.1;SERVER=myServerAddress;DATABASE=myDataBase;UID=myUserName;PWD=myPassword"

'Collect data from form
formdate1 = Request.Form("date1")
formdate2 = Request.Form("date2")
sel = Request.Form("select")

'SQL for Query
query = "SELECT Day_Date, " & sel 
query = query & " FROM(GS_GH INNER JOIN GS_YVC ON GS_GH.[GH_Date] = GS_YVC.[YVC_Date]) INNER JOIN ((GS_WS INNER JOIN GS_JS ON GS_WS.[WS_Date] = GS_JS.[JS_Date]) INNER JOIN ((DAY_DATA INNER JOIN ATTENDANCE ON DAY_DATA.[Day_Date] = ATTENDANCE.[Attendance_Date]) INNER JOIN GS_SE ON ATTENDANCE.[Attendance_Date] = GS_SE.[SE_Date]) ON GS_WS.[WS_Date] = GS_SE.[SE_Date]) ON GS_YVC.[YVC_Date] = GS_SE.[SE_Date]"
query = query & " WHERE Day_Date BETWEEN #" & formdate1 & "# AND #" & formdate2 & "#;"
'query = "SELECT * FROM ATTENDANCE, DAY_DATA, GS_JS WHERE Attendance_Date = Day_Date AND Day_Date = JS_Date;"

'Create and open connection to database
Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open myDSN

'Create recordset
Set rs = Server.CreateObject("ADODB.recordset")

On Error Resume Next 

'Open recordset run query in database
rs.Open query, Connect, adOpenStatic

'Display message
If Err.Number = 0 Then
    Response.Write("<h1>Query Results.</h1>")
    Dim i, fCount
    Response.Write ("<table border='1'><tr>")
    For i = 0 To rs.Fields.Count - 1
        Response.Write ("<th>" & rs.Fields(i).Name & "</th>")
    Next
    Response.Write ("</tr>")
    If Not rs.BOF THEN rs.MoveFirst
        fCount = rs.Fields.Count
        Do Until rs.EOF
            Response.Write ("<tr>")
            For i = 0 To fCount - 1
                Response.Write ("<td>" & rs.Fields(i).Value & "</td>")
            Next
            Response.Write ("</tr>")
            Response.Flush
            rs.MoveNext
        Loop
        If Not rs.BOF THEN
            rs.MoveFirst
        Else
            Response.Write ("<tr><td colspan=""" & fCount & """>No Data Found</td</tr>")
        End If
        Response.Write ("</table><br>")
Else
    Response.Write("<h2>There was an error recording your response!</h2>")
    Response.Write("<h3>Please review your input for format errors. If necessary, contact system administrator.</h3>")
End If
%>

Sorry, I know this is a lot of code. The query string commented out is for testing if I can even pull from the database. That and other simple queries will work without issue. I suspect I am doing my Inner Joins incorrectly and don't know what else to try. If anyone has any suggestion I would be grateful.

Thanks for reading!

0

There are 0 answers