title: Load data from SQL into listbox in HTA (VBscript)
body: <p>I have an HTA (html application) which recieves data from a SQL query, but when I try to paste the results into a listbox it only gives the last record.</p>
  <pre><code>Dim connect, sql, resultSet, pth, txt
  Set connect = CreateObject(&quot;ADODB.Connection&quot;)
      connect.ConnectionString = &quot;Driver={SQL Server};Server=XXX;Database=XXX;trusted_connection=True;&quot; 
      connect.Open
  
  sql = &quot;SELECT [Var0],[Var1],[Var2],[Var3], [Var4], [Var5] FROM [XXX].[dbo].[Table]&quot;
  sql = sql &amp; &quot; WHERE [Var3]='&quot; &amp; FieldUser.value &amp; &quot;' and [Var0]='&quot; &amp; YEAR(Date()) &amp; &quot;-&quot; &amp; RIGHT(&quot;00&quot;&amp;MONTH(Date()),2) &amp; &quot;-&quot; &amp; RIGHT(&quot;00&quot;&amp;DAY(Date()),2) &amp; &quot;'&quot;
  sql = sql &amp; &quot; ORDER BY [Var0] desc ,[Var1] desc&quot;
  
  Set resultSet = connect.Execute(sql)
  
  On Error Resume Next
  resultSet.MoveFirst
  Do until resultSet.eof 
  
  objOption3.Text = &quot;&quot;
  
  if resultSet(0) &lt;&gt; &quot;&quot; then 
  
      'msgbox(resultSet(0) &amp; &quot; ~ &quot; &amp; resultSet(1) &amp; &quot; ~ &quot; &amp; resultSet(2) &amp; &quot; ~ &quot; &amp; resultSet(3) &amp; &quot; ~ &quot; &amp; resultSet(4) &amp; &quot; ~ &quot; &amp; resultSet(5))
      objOption3.Text = resultSet(1) &amp; &quot; ~ &quot; &amp; resultSet(3)
      objOption3.Value = resultSet(0) &amp; &quot;;&quot; &amp; resultSet(1) &amp; &quot;;&quot; &amp; resultSet(2) 
      RegListView.Add (objOption3)
  end if
  
    resultSet.MoveNext
  Loop
  
  
  resultSet.Close
  connect.Close
  Set connect = Nothing
  </code></pre>
  <p>When I uncomment the MsgBox I can see that all records are found, but it only output the last record (it seems to itterate the query correct but only do the Add action when the connection is Closed?)</p>
  <p><strong>How can I make it input all records in the listbox?</strong></p>
  <p>I have tried to change the Add line without succes - I changed to:</p>
  <pre><code>document.all.RegListView.add(objOption3)
  </code></pre>
  <p>I have also tried to output the resultSet as an array - and I can make an msgbox show the records in the array correct but it is not added in the listbox - maybe someone can help me solve it this way?</p>
  <p>Inside the connection:</p>
  <pre><code>dim dbarray
  dbarray = resultSet.getrows
  </code></pre>
  <p>After the connection is closed:</p>
  <pre><code>for i=1 to 100
  
      objOption3.Text = &quot;&quot;
      objOption3.Value = &quot;&quot;
      
  if dbarray(0,i) &lt;&gt; &quot;&quot; then
      msgbox (dbarray(1,i) &amp; &quot; ~ &quot; &amp; dbarray(3,i))
      objOption3.Text = dbarray(1,i) &amp; &quot; ~ &quot; &amp; dbarray(3,i)
      objOption3.Value = dbarray(0,i) &amp; &quot;;&quot; &amp; dbarray(1,i) &amp; &quot;;&quot; &amp; dbarray(2,i) 
      RegListView.Add (objOption3)
  end if
  next
  </code></pre>
  <p>I really hope for some help :)</p>
  <p>ADDED:
  This is the HTML body of the HTA:</p>
  <pre><code>&lt;body&gt;
  
  &lt;table style=&quot;width:100%&quot;&gt;
  &lt;h1&gt;OpgaveTid for &lt;span id=&quot;TextUser&quot;&gt;&lt;/span&gt;&lt;span style=&quot;padding-left:90px&quot;&gt;&lt;input type=&quot;button&quot; value=&quot;Mindre&quot; id=&quot;SendBtnMinimer&quot; style=&quot;width: 50px;&quot; onclick=&quot;vbscript:Minimeropgavetid()&quot;/&gt;&lt;/h1&gt;
  
  
  &lt;tr&gt;
  &lt;td&gt;
  &lt;label for=&quot;FieldSearchOrg&quot;&gt;Search Org:&lt;/label&gt;&lt;br&gt;
  &lt;input type=&quot;text&quot; id=&quot;FieldSearchOrg&quot; name=&quot;FieldSearchOrg&quot; onchange=&quot;vbscript:FuncSearchOrg()&quot; tabindex=&quot;1&quot; size=8&gt;
  &lt;input type=&quot;button&quot; value=&quot;Save&quot; id=&quot;SendBtn&quot; onclick=&quot;vbscript:SaveAction()&quot; tabindex=&quot;3&quot;/&gt;
  &lt;input type=&quot;button&quot; value=&quot;Part&quot; id=&quot;SendBtn&quot; onclick=&quot;vbscript:SavePartAction()&quot; tabindex=&quot;4&quot;/&gt;
  &lt;input type=&quot;hidden&quot; id=&quot;FieldOrgText&quot; name=&quot;FieldOrgText&quot; readonly=&quot;yes&quot;&gt;
  &lt;input type=&quot;hidden&quot; id=&quot;FieldUser&quot; name=&quot;FieldUser&quot; readonly=&quot;yes&quot;&gt;
  &lt;input type=&quot;hidden&quot; id=&quot;FieldUserGroup&quot; name=&quot;FieldUserGroup&quot; readonly=&quot;yes&quot;&gt;
  &lt;/td&gt;
  &lt;td&gt;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;label for=&quot;OrgListView&quot;&gt;Orglist:&lt;/label&gt;&lt;br&gt;
  &lt;select size=&quot;28&quot; name=&quot;OrgListView&quot; style=&quot;width:160&quot; multiple=&quot;no&quot; onchange=&quot;vbscript:FuncSelectOrg()&quot; tabindex=&quot;2&quot;&gt;&lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;label for=&quot;RegListView&quot;&gt;My registrations:  (Tryk F5 for at opdatere)&lt;/label&gt;&lt;br&gt;
  &lt;select size=&quot;14&quot; name=&quot;RegListView&quot; style=&quot;width:160&quot; multiple=&quot;no&quot; onchange=&quot;vbscript:FuncSelectHistReg()&quot;&gt;&lt;/select&gt;&lt;br&gt;&lt;br&gt;
  &lt;input type=&quot;button&quot; value=&quot;Edit registration&quot; id=&quot;UpdateReg&quot; style=&quot;width: 150px;&quot; onclick=&quot;vbscript:UpdateReg()&quot;/&gt;
  &lt;input type=&quot;button&quot; value=&quot;Delete registration&quot; id=&quot;DeleteReg&quot; style=&quot;width: 150px;&quot; onclick=&quot;vbscript:DeleteReg()&quot;/&gt;&lt;br&gt;&lt;br&gt;
  &lt;/td&gt;
  &lt;td&gt;
  
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  </code></pre>