<html>
<head>
  <title>SQL Console | .\SQLEXPRESS01 caseOrganizer | M Square Consulting</title>
  <HTA:APPLICATION
    ID="caseConsole"
    APPLICATIONNAME="CaseOrganizerConsole"
    BORDER="thin"
    CAPTION="yes"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    WINDOWSTATE="normal"
    SCROLL="yes"
    MAXIMIZEBUTTON="yes"
    MINIMIZEBUTTON="yes"
  >
  <style>
    body {
      font-family: monospace;
      background-color: #f4f4f4;
      padding: 40px;
    }

    textarea, #output {
      width: 100%;
      padding: 10px;
      margin-top: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      box-sizing: border-box;
    }

    button {
      margin-top: 10px;
      padding: 8px 16px;
      font-size: 14px;
      cursor: pointer;
    }

    #output {
      background: #eee;
      white-space: pre-wrap;
    }
  </style>
</head>

<body>
  <h2>SQL Console | .\SQLEXPRESS01 caseOrganizer</h2>
  <textarea id="query" rows="6" placeholder="e.g., SELECT * FROM caseFiles;"></textarea>
  <button onclick="runSQL()">Execute</button>
  <div id="output">Console output will appear here.</div>

  <script>
    window.resizeTo(1000, 700);
  </script>

  <script language="VBScript">
    Sub runSQL()
      Dim conn, rs, sql, result
      sql = Document.getElementById("query").value

      Set conn = CreateObject("ADODB.Connection")
      conn.Open "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS01;Initial Catalog=caseOrganizer;Integrated Security=SSPI"

      On Error Resume Next
      Set rs = conn.Execute(sql)

      If Err.Number <> 0 Then
        Document.getElementById("output").innerHTML = "Error: " & Err.Description
        Exit Sub
      End If

      result = ""
      If Not rs.EOF Then
        For Each field In rs.Fields
          result = result & field.Name & vbTab
        Next
        result = result & "<br>"

        Do Until rs.EOF
          For Each field In rs.Fields
            result = result & field.Value & vbTab
          Next
          result = result & "<br>"
          rs.MoveNext
        Loop
      Else
        result = "Command executed. No rows returned."
      End If

      Document.getElementById("output").innerHTML = result
      rs.Close
      conn.Close
    End Sub
  </script>
</body>
</html>