title: VbScript ADODB.RecordSet RecordCount returns -1
body: <p>My question today is a rather simple one. What I have is a VB Module that contains the code to return me an ADODB.RecordSet object with records fetched from a SQL Query that has been executed. It works like this:</p>
  
  <pre><code>sSql_SerCheck = "SELECT DISTINCT Serial FROM dbo.WipReservedSerial WHERE  Serial LIKE '" &amp; serialTempSearch
  sSql_SerCheck = sSql_SerCheck &amp; "' ORDER BY Serial DESC "
  dbGetRecordSet(sSql_SerCheck)
  </code></pre>
  
  <p>Then the results sit in object rs that is accessed like the following </p>
  
  <pre><code> temp = rs(0) 'For the value at the first column for the first record
   rs.MoveNext  'This moves to the next record in the record set
  </code></pre>
  
  <p>Now what I am trying to do here is to the number of records contained within this recordset object. Now I did some research on the class and found there is a
  RecordCount att. </p>
  
  <p>So what I want to do is simple:</p>
  
  <pre><code>if( rs.RecordCount &gt; 0) then
      serCheck1 = rs(0)
      MsgBox serCheck1
  end if
  </code></pre>
  
  <p>The problem is my RecordCount returns -1. I found this article <a href="http://www.w3schools.com/asp/prop_rs_recordcount.asp" rel="noreferrer">http://www.w3schools.com/asp/prop_rs_recordcount.asp</a> that states that record count will return -1 for the following:</p>
  
  <p>Note: This property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and -1 or the actual count for a dynamic cursor.</p>
  
  <p>Note: The Recordset object must be open when calling this property. If this property is not supported it will return -1.</p>
  
  <p>How do I get this object to return the correct number of records??</p>
  
  <p>The code for the VB Module is added below: </p>
  
  <pre><code>Public cn, rs
  
  
  'Specify pSQL as SQL Statement
  Function dbGetRecordset(sSql)
  dbCloseConnection()
  
  Set cn = CreateObject("ADODB.Connection")
  cn.CommandTimeout = 600
  cn.Open(Conn &amp; SystemVariables.CodeObject.CompanyDatabaseName)
  Set rs = CreateObject("ADODB.Recordset")
  rs.Open sSql, cn, 3, 3
  End Function
  </code></pre>