Need help using ADO to return a range of values

bfreescott

Board Regular
Joined
Aug 6, 2015
Messages
115
Below is a function into which I pass a filename with it's full path and a sheet name. I have used this function successfully with a named range in the past, but not with a sheet name and explicit range. It is not opening the recordset. When I use the immediate window to determine rs.Fields(0).Name, I get the value of C13, but that is the extent of what seems to be working.


Code:
Function Get_VPK_Allot(var1 As String, var2 As String) As Variant
'
    On Error GoTo ErrorHandler

    Dim Conn As ADODB.Connection
    Dim ConnString As String
    Dim rs As New ADODB.Recordset
    Set Conn = New ADODB.Connection

    ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & var1 & ";Extended Properties=Excel 12.0 Xml"
    Conn.Open ConnString

    If Not (Conn.State And 1) = 1 Then
        MsgBox "Source data cannot be found. Please check the location of the file you are querying."
        Set Conn = Nothing
        Set rs = Nothing
        Application.ScreenUpdating = True
        Application.StatusBar = False
        Exit Function
    End If

    rs.Open "SELECT * FROM [" & var2 & "$C13:K13];", Conn, adOpenStatic, adLockReadOnly

    If Not rs.BOF = True And rs.EOF = True Then
        Get_VPK_Allot = rs.Fields(0).Value
    End If

    rs.Close
    Conn.Close
    Set Conn = Nothing
    Set rs = Nothing

Exit Function
ErrorHandler:
    MsgBox "Error: " & Err.Number & " , " & Err.Description & Chr(13) & _
    "Procedure is: Get_VPK_Allot" & Chr(13) & ""
    rs.Close
    Conn.Close
    Set Conn = Nothing
    Set rs = Nothing
    Application.ScreenUpdating = True
    Application.StatusBar = False
'
End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The query you've passed is trying to get data only from one row - columns C to K of line 13.

Code:
rs.Open "SELECT * FROM [" & var2 & "$[COLOR=#0000ff]C13:K13[/COLOR]];", Conn, adOpenStatic, adLockReadOnly

So you're getting a recordset with no records - Top row of range is assumed to be headers.

If you want to query only a specific portion of the spreadsheet then specify the complete range (e.g. C13:K1000).
Code:
rs.Open "SELECT * FROM [" & var2 & "$[COLOR=#0000ff]C13:K1000[/COLOR]];", Conn, adOpenStatic, adLockReadOnly

Otherwise if you want to query the entire sheet as is, then you can just write "SheetName$"
Code:
rs.Open "SELECT * FROM [" & var2 & "$];", Conn, adOpenStatic, adLockReadOnly
 
Last edited:
Upvote 0
Thanks. I made the same mistake when using named ranges before. With ADO, when you want a specific range, you must use the row immediately preceding the row you want to return values from as your first row in the range you query. I changed C13 to C12 and now it works.
 
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,439
Members
449,160
Latest member
nikijon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top