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.
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.