VBcoders Guest

Don't have an account yet? Register

Forgot Password?

A basic ADO Open and Requery routine

by Steve Mann (1 Submission)
Category: Databases/Data Access/DAO/ADO
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (12 Votes)

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.

rs - an ADO recordset (eg Dim rsMine as New ADODB.Recordset) szSource - (eg "select * from customers") Optional - bReadOnly (True for read-only)
Assumes your public ADO Connection object is called gCn, and that the supplied szSource is a valid SQL statement.
Code Returns
Sets the supplied Recordset.
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.

Rate A basic ADO Open and Requery routine

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)
End If
 Exit Sub
 MsgBox Err.Description
 GoTo lab_Exit
End Sub

Download this snippet    Add to My Saved Code

A basic ADO Open and Requery routine Comments

No comments have been posted about A basic ADO Open and Requery routine. Why not be the first to post a comment about A basic ADO Open and Requery routine.

Post your comment

0/1000 characters