PDA

View Full Version : Can't return data to recordset



jjc99
10-20-2011, 03:35 AM
Hi all

I am having trouble returning data to a recordset.

My code is as follows

Dim oCon As ADODB.Connection
Dim oRS As ADODB.Recordset
Set oCon = New ADODB.Connection
oCon.ConnectionString = "Provider=SQLNCLI10;Server=PC-000\SQLEXPRESS;Database=MYDB; Trusted_Connection=yes;"
oCon.Open
Set oRS = New ADODB.Recordset
oRS.ActiveConnection = oCon
oRS.Open ("SELECT * FROM tbl_STUFF")
MsgBox oRS.RecordCount
oRS.Close
oCon.Close
If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCon Is Nothing Then Set oCon = Nothing


The connection appears to be valid as I do not get any errors. However, no data appears to be returned to the dataset.

Any help would be greatly appreciated.

hansup
10-20-2011, 02:41 PM
In some situations, RecordCount is not reliable until the recordset has been fully populated. You can force that to happen by inserting this line before the MsgBox:

oRS.MoveLast

mohanvijay
10-21-2011, 01:14 AM
for get recordcount open a recordset cursor as "adOpenStatic" or "adOpenKeyset"

the default cursor is "adOpenForwardOnly" which is always return -1



oRS.Open "SELECT * FROM tbl_STUFF", oCon, adOpenStatic, adLockReadOnly