Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>ADO Recordset has <code>.State</code> property, you can check if its value is <code>adStateClosed</code> or <code>adStateOpen</code></p> <pre><code>If Not (rs Is Nothing) Then If (rs.State And adStateOpen) = adStateOpen Then rs.Close Set rs = Nothing End If </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms675068%28v=vs.85%29.aspx">MSDN about State property</a></p> <p>Edit; The reason not to check <code>.State</code> against 1 or 0 is because even if it works 99.99% of the time, it is still possible to have <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms675546%28v=vs.85%29.aspx">other flags set</a> which will cause the If statement fail the <code>adStateOpen</code> check.</p> <p>Edit2:</p> <p>For Late binding without the ActiveX Data Objects referenced, you have few options. Use the value of adStateOpen constant from <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms675546%28v=vs.85%29.aspx">ObjectStateEnum</a></p> <pre><code>If Not (rs Is Nothing) Then If (rs.State And 1) = 1 Then rs.Close Set rs = Nothing End If </code></pre> <p>Or you can define the constant yourself to make your code more readable (defining them all for a good example.)</p> <pre><code>Const adStateClosed As Long = 0 'Indicates that the object is closed. Const adStateOpen As Long = 1 'Indicates that the object is open. Const adStateConnecting As Long = 2 'Indicates that the object is connecting. Const adStateExecuting As Long = 4 'Indicates that the object is executing a command. Const adStateFetching As Long = 8 'Indicates that the rows of the object are being retrieved. [...] If Not (rs Is Nothing) Then ' ex. If (0001 And 0001) = 0001 (only open flag) -&gt; true ' ex. If (1001 And 0001) = 0001 (open and retrieve) -&gt; true ' This second example means it is open, but its value is not 1 ' and If rs.State = 1 -&gt; false, even though it is open If (rs.State And adStateOpen) = adStateOpen Then rs.Close End If Set rs = Nothing End If </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload