VB icon

A basic ADO Open and Requery routine

Email
Submitted on: 1/9/2015 5:49:00 AM
By: Steve Mann (from psc cd)  
Level: Advanced
User Rating: By 13 Users
Compatibility: VB 6.0, VB Script, ASP (Active Server Pages)
Views: 1668
 
     New to ADO? Worried about ADO? This little subroutine gets round the problems of opening ADO recordsets. Please look elsewhere on this site for info on opening the database itself. If you open a Recordset in your code, ADO expects you to close it before re-opening. But if it's not open you can't close it... (Oh My!). Here's my solution. It requires a public ADODB.Connection - I call it gCn. The routine will open a new recordset (compatible with Janus GridEx), or refresh it if it's open or if the SQL has changed. Optional ReadOnly argument.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: A basic ADO Open and Requery routine
' Description:New to ADO? Worried about ADO? This little subroutine gets round the problems of opening ADO recordsets. Please look elsewhere on this site for info on opening the database itself.
If you open a Recordset in your code, ADO expects you to close it before re-opening. But if it's not open you can't close it... (Oh My!).
Here's my solution. It requires a public ADODB.Connection - I call it gCn.
The routine will open a new recordset (compatible with Janus GridEx), or refresh it if it's open or if the SQL has changed. Optional ReadOnly argument.
' By: Steve Mann (from psc cd)
'
' Inputs:rs - an ADO recordset (eg Dim rsMine as New ADODB.Recordset)
szSource - (eg "select * from customers")
Optional - bReadOnly (True for read-only)
'
' Returns:Sets the supplied Recordset.
'
' Assumes:Assumes your public ADO Connection object is called gCn, and that the supplied szSource is a valid SQL statement.
'
' Side Effects:If you pass invalid SQL, you'll get an error. Ctrl+Break and you'll be ready to F8 out and see where you went wrong.
'**************************************

Public Sub ADO_OpenRs(rs As Recordset, szSource$, Optional bReadOnly = False)
' Open or Requery a Recordset.
On Error GoTo lab_Err
If rs.State = adStateClosed Or rs.Source <> szSource Then
 If rs.State <> adStateClosed Then rs.Close
 rs.Open szSource, gCn, adOpenStatic, IIf(bReadOnly, adLockReadOnly, adLockOptimistic)
Else
 rs.Requery
End If
lab_Exit:
 
 Exit Sub
 
lab_Err:
 
 MsgBox Err.Description
 GoTo lab_Exit
 
End Sub


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.