سؤال

I've facing a boring problem over here...

i'm trying to cast a recordset into array, but the object type is not returned correctl

I've a Recordset type function, and i try to receive the results of this function by the recordset object. But when i try to test the object type, the typeof function returns "Fields" type.

Search Method:

Public Function Buscar(ByVal query As String) As Recordset
Dim rs As New Recordset

If con.State <> 1 Then
    Conectar
End If

rs.Open query, con, adOpenStatic, adLockReadOnly
Set Buscar = rs

End Function


dim r as recordset
dim c as new clsConnection

r = c.buscar("select * from costumers")
c.casttoarray(r)

Private Sub castToArray(obj As Object)

Dim rs          As New Recordset
Dim linhas()    As String
Dim colsize()   As Integer
Dim aux         As Integer

If TypeOf obj Is adodb.Recordset Then

    Dim nLin As Long
    Dim nCol As Integer
    Dim l As Long
    Dim c As Integer

    Set rs = obj
    colsize = capturarLimites(rs)
    nLin = rs.RecordCount
    nCol = rs.Fields.Count
    ReDim linhas(nLin)


    rs.MoveFirst
    For l = 0 To nLin - 1
        For c = 0 To nCol - 1
            aux = colsize(c) - Len(rs.Fields(c).Value)
            linhas(l) = linhas(l) & "" & rs.Fields(c).Value & rptString(" ", aux + 1)
        Next c
    Next l

End If



End Sub
هل كانت مفيدة؟

المحلول

The problem is the parentheses used when you call the Sub:

c.casttoarray (r)

These evaluate r and so pass the default member of the Recordset class (which is the Fields collection) to the procedure.

Remove them or use call;

c.casttoarray r
call c.casttoarray(r)

Or strongly type the casttoarray argument.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top