Hi!
I have an excel workbook full with VBA code to get several things done. I need frequent querys to data tables hosted inside the workbook, no RDBMS system in use this time.
However, I've found very convenient to solve this querys using ADODB recordsets. I implement this kind of recordsets with this Function in one of my utility modules
Public Function sqlSelect(camposSelect As String, tabla As Range, camposWhere As String, camposOrder As String, camposGroupBy As String) As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim currAddress As String, strFile As String, strCon As String, strSQL As String
On Error GoTo mal
currAddress = tabla.Parent.Name & "$" & tabla.Address(False, False)
strFile = ThisWorkbook.fullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ReadOnly=True"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT " & camposSelect & " FROM [" & currAddress & "]"
If camposWhere <> "" Then strSQL = strSQL & " WHERE " & camposWhere
If camposOrder <> "" Then strSQL = strSQL & " ORDER BY " & camposOrder
If camposGroupBy <> "" Then strSQL = strSQL & " GROUP BY " & camposGroupBy
rs.Open strSQL, cn, adOpenForwardOnly
'Debug.Print rs.GetString
Set sqlSelect = rs
Exit Function
mal:
MsgBox "Error en sqlSelect: " & Err.description
Stop
Set sqlSelect = Nothing
End Function
And I use this function in code like this:
Private Sub test_sqlSelect()
Dim rs As New ADODB.Recordset
Dim i As Integer
Set rs = sqlSelect("[Campo 2],[Tipo coste],[Campo 1],Importe,Fila", _
wksCostesGEP.Range("tblGEP"), _
"([Tipo coste]='FIN' or [Tipo coste]='COM') and [Pool]='GesDoc' and [Comp_Version]='Filenet·SegurCaixa'", "", "")
Do While Not rs.EOF
For i = 0 To rs.Fields.Count - 1
Debug.Print rs(i).Name & "= " & rs(i).value
Next i
rs.MoveNext
Loop
End Sub
This have been working like a charm for months now, but a couple of days ago I suddenly found a problem. There are some recordsets that return null values for some fields even when the actual value of the cell in the table is not null.
I've made several tests and google for similar issues but found nothing.
Have anyone a clue of what the hell is happening here?
Thank you in advance
Bookmarks