VB6 ADO Disconnected recordset returns no records

Go To StackoverFlow.com

1

I am creating, opening and then disconnecting a recordset against a MySQL database. This works correctly for one query but returns 0 records for another query where the rows exist in the database. Where it works, I can delete the records from the recordset as well.

The query that returns records:

sql = "select convert(v.wonotes using UTF8) as WonData, v.wo_cat_id, v.id As wo_desc_id, v.line_no as line_no, " & _
      " v.wo_id as wo_id, v.prop_id as prop_id, convert(v.description using UTF8) as description, v.cat_id as cat_id, " & _
      " v.completion_date as completion_date from vw_property_wo_desc v " & _
      " where v.wo_cat_id= 6 and **(v.wo_status = 'completed' or v.wo_status = 'approved')** " & _
      " and v.wonotes is not null and v.wonotes<> '' "

The query that does NOT return records:

sql = "select convert(v.wonotes using UTF8) as WonData, v.wo_cat_id, v.id As wo_desc_id, v.line_no as line_no, " & _
      " v.wo_id as wo_id, v.prop_id as prop_id, convert(v.description using UTF8) as description, v.cat_id as cat_id, " & _
      " v.completion_date as completion_date from vw_property_wo_desc v " & _
      " where v.wo_cat_id= 6 and **v.wo_status = 'unassigned'** " & _
      " and v.wonotes is not null and v.wonotes<> '' "

There is no other change.

If I change the cursor type to adOpenDynamic, the query returns records, but I cannot then disconnect it. This is just to prove that the database has records satisfying that query.

Code for disconnected recordset:

With rsToUse

    If .State = adStateOpen Then .Close

    .ActiveConnection = GetConnection
    .Source = sql
    .CursorLocation = adUseClient
    .CursorType = adOpenForwardOnly
    .LockType = adLockBatchOptimistic
    .Open

    If .EOF Then
        .Close
        Exit Function
    End If

    .ActiveConnection = Nothing

End With

I have run out of ideas, please help.

2012-04-05 16:19
by Shrieks
I think you mean to say you are opening and then disconnecting a Recordset. Have you tried using a static cursor type - Bob77 2012-04-05 17:09
@BobRiemersma: Updated the question with the correct text. Yes, I have tried with Static, but still does not return records. The moment I set the cursorside to Client, it stops returning records. If set to Server, records are returned - Shrieks 2012-04-05 17:42
One more observation - if set the cursortype to adForwardOnly and serverside, I start getting nulls in the BLOB field. Using static seems to work - but still server side - Shrieks 2012-04-05 17:52
For all we know you are running into a limition of the Provider you are using. If you are relying on the clunky MySQL ODBC Driver that could be an issue as well (and it has to thunk through the adapter layer MSDASQL too). Can/have you tried this will a full-featured OLEDB Provider for MySQL - Bob77 2012-04-06 17:17
I would tend to agree about the limitation (whatever it is), except that the earlier query works for a disconnected recordset. It will be too much work to change the database driver since the code is used widely. Any ways, I have limited the number of records it will return from the Dynamic cursor and left it as Dynamic. Thank - Shrieks 2012-04-06 17:23
If you set the cursorlocation to adUseClient (assuming it is supported by the OLE DB Provider in use), the CursorType will be forced to Static no matter what it is explicitly set to. Cursorlocation takes priority over CursorType - BobRodes 2012-04-13 14:51


0

This is the code I use to get disconnected recordsets from a SQL Server database. I suspect it would also work for a MySQL database (except for the connection string, of course).

Public Function GetRecordset(ByVal SQL As String) As ADODB.Recordset

    Dim DB As ADODB.Connection
    Dim RS As ADODB.Recordset

    Set DB = CreateObject("ADODB.Connection")
    DB.ConnectionString = globalConnectionString
    DB.CursorLocation = adUseClient
    DB.CommandTimeout = 0
    Call DB.Open

    Set RS = CreateObject("ADODB.Recordset")
    RS.CursorLocation = adUseClient
    Call RS.Open(SQL, DB, adOpenForwardOnly, adLockReadOnly)
    Set RS.ActiveConnection = Nothing
    Set GetRecordset = RS
    Set RS = Nothing
    DB.Close
    Set DB = Nothing

End Function

This exact code has been in production for at least 5 years now without any issue. I encourage you to give it a try.

I think the magic combination to using a disconnected recordset is to make sure the connection object has the CursorLocation set to UseClient, and the recordset object is ForwardOnly and LockReadOnly.

2012-04-05 21:41
by G Mastros
Thanks - I will give this a try tomorrow - Shrieks 2012-04-05 22:24
I tried this but still no records. By the way, setting the CursorLocation to Client at Connection level sets the default of the Recordset to client as well. I hate to say this - but it seems weird because the first query works without a problem - Shrieks 2012-04-06 16:03
Ads