在 VB 上使用 PS 脚本执行访问查询并在 HTA 文本框中写入输出

Doing an Access Query with PS Script over VB and writing output in HTA Textbox

我是 PowerShell 新手,遇到了以下任务。
我必须创建一个 HTA GUI,您可以在其中写入要在访问查询中搜索的名称。
HTA 文件中的 VB 脚本启动 PS 脚本,并在 HTA 文本框中传递用户输入的参数。之后,PowerShell 脚本使用用户输入执行访问查询以获得一些结果。这些结果应该以某种方式返回到 VB/HTA 文件,以便它可以将每个结果输出到另一个文本框。

这甚至可以做到吗?如果是,我将不胜感激一些解决方案。

编辑:

VB/HTA

VB/HTA

Wrong Format, should be like a table

是的,这是可能的。使用具有 3 个字段 first_name、last_name、电子邮件

的访问数据库尝试下面的代码

将文件另存为 Employees.mdb(您可以使用 .accdb,但必须在代码中更改名称)确保访问文件与 HTA 在同一文件夹中。

我从 www.mockaroo.com 复制了一些数据来测试它(我没有得到报酬说它非常有用)

这是一个非常基本的示例,但可以在 HTA 中创建比在 Access 中更好的 GUI。

<title>Employee Directory</title>
<head>
<HTA:APPLICATION
ID="EMPDIR"
APPLICATIONNAME="Employee Directory"
SINGLEINSTANCE="YES"
>
<!-- makes the hta run using IE9 otherwise it runs as IE5 or 6.  You can change this to edge -->
<meta http-equiv="x-ua-compatible" content="IE=9"/>

<style>

body {font-family:arial; background:#efefef; color:#333}


</style>


<script language="vbscript">
' ///// this creates the connection when the file is first run.

' //// get the current path if the database file is in the same folder as the hta file.  if it's not then enter the full path manually below
Set objFSO = CreateObject("Scripting.FileSystemObject")
curDir = objFSO.GetAbsolutePathName(".") & "\"


Dim oCon: Set oCon = CreateObject("ADODB.Connection")
Dim oRs: Set oRs = CreateObject("ADODB.Recordset")
strCon = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq="& curDir & "Employees.mdb;"

oCon.ConnectionString= strCon

sub getEmployeeList
' gets a list of employees based on the value in the searchbox input.

oCon.open

strSQL = "SELECT * FROM Employees WHERE (first_name like '%" & searchbox.value & "%') or (last_name like '%" & searchbox.value & "%')"

Set oRs = oCon.Execute(strSQL)
strHTML = "<table>"
do while not oRs.EOF

strHTML = strHTML & "<tr><td>" & oRs.fields("first_name") & "</td><td>" & oRs.fields("last_name") & "</td><td>" & oRs.fields("email") & "</td></tr>"

oRs.movenext
loop

oCon.close

strHTML = strHTML & "</table>"

divEmployeeList.innerHTML = strHTML

end Sub

</script>


</head>

<body>

<div id="search"><input type="text" id="searchbox" style="font-size:16pt; margin:10px;"/> <input type="button" value="search" name="submitsearch" style="font-size:16pt;" onclick="getEmployeeList" language="vbscript"></div>


<div id="divEmployeeList" style="width:90%; height:300px; overflow-y:scroll; border:solid 1px #666; margin:10px; background:#fff">-</div>


</body>

您在读取文本文件的 HTA 代码中有 strLine。您的代码正在读取文本文件并取 1 行并将其放入文本输入框中。为了将结果显示为 table,您需要创建 table HTML 并将 HTML 传递给 div 元素。

strLine = "<table>"
Do While Not strLines.AtEndOfStream
   strLine = strLine & "<tr><td>" & strLines.ReadLine() & "</td></tr>"
Loop
strLine = strLine & "</table>"
Weiterleitung_div.innerHTML = strLine

您需要更改 Weiterleitung_id.value = strLine 并将 strLine HTML 传递给正文中的元素。添加到正文,然后