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