Results 1 to 3 of 3

Thread: BOF/EOF Error using GetRows() with 1 record

  1. #1

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    BOF/EOF Error using GetRows() with 1 record

    Using ADO within Excel (2013) VBA to extract records from an Access file. For some reason I get a BOF/EOF error when using GetRows() if only one record (i.e. RecordCount = 1) is in the recordset. I've dug around and found nothing about a limitation of GetRows() in case of 1 record only. I can certainly test for Recordcount=1 and copy the field values in a loop into the array rather than use GetRows() but that seems like a goofy workaround. Am I doing someone wrong?

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: BOF/EOF Error using GetRows() with 1 record

    Am I doing someone wrong?
    hard to say without seeing your code. are you doing some loop around the getrows? then i'd say you loop once too often because i tried getrows one a single record and this works nicely:

    Code:
    Sub a()
        Dim cn As New Connection
        cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database1.accdb;Persist Security Info=False;"
        Dim rs As Recordset
        Set rs = cn.Execute("SELECT TOP 1 * FROM tbl1")
        Dim vFieldValues() As Variant
        vFieldValues = rs.GetRows
        cn.Close
    End Sub
    while this fails:
    Code:
    Sub a()
        Dim cn As New Connection
        cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Database1.accdb;Persist Security Info=False;"
        Dim rs As Recordset
        Set rs = cn.Execute("SELECT TOP 1 * FROM tbl1")
        Dim vFieldValues() As Variant
        vFieldValues = rs.GetRows
        vFieldValues = rs.GetRows
        cn.Close
    End Sub

  3. #3

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: BOF/EOF Error using GetRows() with 1 record

    Thanks for your reply. I tried a test and got the same result as you, which means I must somehow be incrementing the recordset pointer to EOF. I now know what to look for.

    Code:
    Sub testADO()
        Dim rsString As String
        Dim num As Long
        Dim v As Variant
        
        rsString = "SELECT TOP 1 * FROM " & strTable
        Set cn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        cn.Provider = strPROVIDER
        cn.Properties("Data Source") = [rFileDB].Value
        cn.Open
        rs.Open rsString, cn, adOpenStatic, adLockOptimistic, adCmdText
        num = rs.RecordCount
        v = rs.GetRows
        rs.Close
        cn.Close
        Set rs = Nothing
        Set cn = Nothing
        
        Debug.Print "num=" & num, "LB=" & LBound(v, 2), "UB=" & UBound(v, 2)
        'num=1         LB=0          UB=0'
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width