<%
    Dim Quit

    Dim FSO
    Dim ConString
    Dim Cat
    Dim Conn
    Dim RS

    Set FSO = Server.CreateObject("Scripting.FileSystemObject")

    Set Cat = Server.CreateObject("ADOX.Catalog")
    Set Conn = Server.CreateObject("ADODB.Connection")
    Set RS = Server.CreateObject("ADODB.Recordset")


    Dim path

    path = request.Form("txtPath")



    If FSO.FileExists(path) Then
        ConString = "Provider='Microsoft.Jet.OLEDB.4.0';data source='" & path & "';"
        Conn.Open (ConString)
        Cat.ActiveConnection = Conn

        Quit = False
    Else
        Quit = True
        response.write "<center><h3><font color=red>Path dont exist</font></h3></center>"

    End If
%>

<html>
<head>
<TITLE>SQL</TITLE>

<LINK href="styles.css" type=text/css rel=stylesheet>
<script language="JavaScript">

Function Validate()
{
    return true;
}

Function SetSQL()
{
    var i;
    var d;
    d=document.form1;

    i=d.Tables.selectedIndex;

    if (i==-1)
        return;

    document.form1.showRS[0].checked=true;

    document.form1.txtSQL.value="Select * From [" + document.form1.Tables.options[i].value + "]";


    document.form1.submit();
}


</script>

</head>
<%


Dim showresult
Dim showRS
Dim path

If Quit = False Then
    ConString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & path
End If

sql = Trim(request.Form("txtSQL"))
showRS = request.Form("showRS")


If Quit = False Then
    If sql <> "" Then
        Set Conn = Server.CreateObject("ADODB.Connection")
        Set RS = Server.CreateObject("ADODB.Recordset")

        Conn.Open (ConString)
        Set RS = Conn.execute(sql)

        If showRS = "yes" Then

            If RS.EOF = False Then
                showresult = True
            Else
                showresult = False
            End If
        End If
    End If
End If

%>

<BODY bgcolor="#FFFFFF">
<center>
  <form name=form1 method=post action="" onsubmit="return Validate()">

    <table border="0" cellpadding="1" cellspacing=1 width="98%" height="386" collapse" bgcolor="lightblue">
      <tr>
        <td class=tableborder width="43%" colspan="2" align="center"  bgcolor=lightblue height="13">SQL</td>
        <td width="19%" align="center" class=tableborder bgcolor=lightblue height="13">Table Name</td>
      </tr>
      <tr>
        <td width="43%" colspan="2" height="200" bgcolor="white">
          <textarea name="txtSQL" cols="50" rows="15" style="width:100%;height:100%"><%=sql%>
          </textarea>
        </td>
        <td width="19%" height="294" bgcolor="#FFFFFF" valign="top">

        <select styles="Width:100%" name="Tables" size="22">
        <%
        If Quit = False Then
            For i = 0 To Cat.Tables.Count - 1
                If Cat.Tables(i).Type = "TABLE" Then
                    response.write "<option value=" & Cat.Tables(i).Name & ">" & Cat.Tables(i).Name & " (Table)" & "</option>"
                ElseIf Cat.Tables(i).Type = "VIEW" Then
                    response.write "<option  value=" & Cat.Tables(i).Name & ">" & Cat.Tables(i).Name & " (Query)" & "</<option>"
                End If
            Next
        End If
        %>
        </select>
        </td>
      </tr>
      <tr>
        <td class=tableborder bgcolor=lightblue width="9%" height="22">Path :
        </td>

        <td align=left width="39%" height="22" bgcolor="#FFFFFF" >
          <input style="Width:100%" type="text" name="txtPath" size="50" value="<%=path%>">

        <td align=center width="19%" height="22" bgcolor="#FFFFFF">
        <input type=button value="Show" onclick="JavaScript:SetSQL()">
        </td>
      </tr>

      <tr>
        <td class=tableborder bgcolor=lightblue width="11%" height="19">Database Type :
        </td>
        <td width="32%" height="19" bgcolor="#FFFFFF">
          <select name="select">
            <option value="0">Select One</option>
            <option value="access97">Access 97</option>
            <option value="access2k" selected>Access 2000</option>
            <option value="sqlserver">SQL Server</option>
          </select></td>
        <td width="19%" height="19" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
      <tr>
        <td width="62%" colspan="3" align="center" height="13" bgcolor="#FFFFFF">
        <%If showrs="yes" then%>
            <input type="radio" name="showRS" value="yes" checked>
            Display Result
            <input type="radio" name="showRS" value="no">
            No Restult
         <%else%>
            <input type="radio" name="showRS" value="yes">
            Display Result
            <input type="radio" name="showRS" value="no">
            No Result
        <%End If%> </td>
      </tr>
      <tr>
        <td width="62%" colspan="3" align="center" height="13" bgcolor="#FFFFFF">
            <input type="submit" name="Assign" value="Execute"></td>
      </tr>
    </table>
    <p>&nbsp;</p>
</form>

<%
With response
    .write "<table width='98%' border='0' cellspacing='1' cellpadding='3' bgcolor=black>"
        .write "<tr bgcolor=white>"
        .write "<td align=center class=tableborder bgcolor=lightblue width='20px'>#</td>"

        If Quit = False Then
            For i = 0 To RS.fields.Count - 1
                .write "<td class=tableborder bgcolor=lightblue>"
                .write RS.fields(i).Name
                .write "</td>"
            Next
            .write "</tr>"
        End If

        Dim n
        n = 1
        If Quit = False And showresult Then
            Do While Not RS.EOF
                .write "<tr bgcolor=white>"
                .write "<td align=center>" & n & "</td>"
                For i = 0 To RS.fields.Count - 1
                .write "<td>"
                .write RS.fields(i).Value
                .write "</td>"
                Next
                .write "</tr>"
            RS.movenext
            n = n + 1
            Loop
        Else
            If path <> "" Then
                .write "<tr bgcolor=white><td colspan=100>No Record Found</td></tr>"
            End If
        End If



    .write "</table>"
End With
%>

<a href=logout.asp>Logout</a>
</center>
<p align=center>&nbsp;</p>
</BODY>
</html>

<%

    Set Cat = Nothing
    Set RS = Nothing
    Set Conn = Nothing
%>