I am trying to query an Excel file from another file using VBA and ADO options. When I run this code it's throwing the error "Cannot updata, Database or object Read only":
Public Function fnExecuteXlQuery _
(ByVal strPath As String, _
ByVal strQuery As String) As ADODB.Recordset
Dim rs As ADODB.Recordset
Dim conStr As String
On Error GoTo ErrorHandler
conStr = "Provider=Microsoft.Jet.OLEDB.4.0; " _
& "Data Source=" & strPath & "; Extended Properties=Excel 8.0"
Set rs = New ADODB.Recordset
rs.Open strQuery, conStr, adOpenDynamic
Set fnExecuteXlQuery = rs
Exit Function
ErrorHandler:
Set fnExecuteXlQuery = Nothing
fnDisplayError Error(Err) & "Unable to fetch data from DTS...", ERROR_TYPE_ERROR
End Function
The "strPath" is the source Excel file and the "strquery" has the following SQL code:
Select [Activity],[Name],[Date],[Hours Spent]
from [Time sheet$]
where [Activity] = 'Billable Activities'
Order by Name,date
Maybe all of what has been said in the comments just needs to be written out once more to make sure that you really got the solution right:
Option Explicit
Public Sub ConnectionToExcel()
Dim rstResult As ADODB.Recordset
Dim strConnectin As String
Dim strPath As String
Dim strSQL As String
strPath = "C:\Data\YourFile.xlsm"
strConnectin = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source='" & strPath & "';Extended Properties=""Excel 12.0 XML;HDR=YES;IMEX=1"" "
Debug.Print strConnection
strSQL = "SELECT * FROM [Time sheet$] "
Set rstResult = New ADODB.Recordset
rstResult.Open strSQL, strConnectin, adOpenForwardOnly, adLockReadOnly, adCmdText
Sheet1.Range("A1").CopyFromRecordset rstResult
End Sub
Please note, that I am taking it one step at a time: (1) I am using a sub
instead of a function. (2) The select
has been simplified just to test the connection and can be later expanded upon. (3) Just writing back the result to the first sheet. (4) Further restricting the recordset to be adOpenForwardOnly
only and adLockReadOnly
.
Also, keep in mind that the above code uses early binding and thus requires you to set a reference to Microsoft ActiveX Data Objects 2.8 Library
(or later) in Tools
--> References...
.