I am trying to use a sql query against my Excel workbook but I can not get the connection to open. It fails on the cn.Open strCon line with a Unspecified error Runtime error '-2147467259(80004005)'.
Function Test()
Dim cn As Object 'ADODB.Connection
Dim rs As Object 'ADODB.Recordset
Dim currAddress As String
Dim strSQL As String
Dim strCon As String
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
strSQL = "SELECT * FROM AdelPivot"
rs.Open strSQL, cn
Sql = rs.GetString
End Function
Related
I have this Excel VBA code:
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim query As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=xxxx.xxx.xxxx.xxx,xxxx;" & _
"Initial Catalog=mydb;" & _
"Trusted_Connection=yes;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("")
ActiveSheet.Range("A2").CopyFromRecordset rs
...
End Sub
all works fine.
The issue is in this section: Set rs = conn.Execute("")
My select statement query is just too big, split it into continuation lines is not practical.
Is there a way of reading the query text from a file, or from a variable?
Thank you very much
Sub ConnectSqlServer()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim query As String
' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=xxxx.xxx.xxxx.xxx,xxxx;" & _
"Initial Catalog=mydb;" & _
"Trusted_Connection=yes;"
' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Open the connection and execute.
conn.Open sConnString
'read SQL from file
query = GetContent("C:\Stuff\myQuery.txt")
Set rs = conn.Execute(query)
ActiveSheet.Range("A2").CopyFromRecordset rs
...
End Sub
'read all file content
Function GetContent(f As String) As String
GetContent = CreateObject("scripting.filesystemobject"). _
opentextfile(f, 1).readall()
End Function
Alternatively, store your SQL in a worksheet cell and read it from there.
You don't have to use continuation lines, you can append the strings and append a vbCrLf character. e.g.
Dim sSQL as string
sSQL = "SELECT" & vbCrLf
sSQL = sSQL & "AField" & vbCrLf
sSQL = sSQL & ",BField" & vbCrLf
sSQL = sSQL & ",(SELECT XField FROM AnotherTable ..........) as XField" & vbCrlf
'and so on and so on
Set rs = conn.Execute(sSQL)
You can build very long SQL statements this way.
I get the following error when I try to run an internal sql query in my Excel spreadsheet.
Error 2147467259 - Method of 'Execute' object' _Connection' failed.
Also when I run the code with breakpoints, I can get an automation error. I am querying in Excel 2013 (15.0.5023.1000).
With conn_obj
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & report_name & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
On Error GoTo err_SQL
SQL = query
Set rs = conn_obj.Execute(SQL) 'error here
Update: I replaced the above code with the below, I am getting an error on
Format of the initialization string does not conform to the OLE DB Specification.
Dim sSQLString As String
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
Let DBPath = report_name 'Path here
Let sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath & ";Extended Properties='text;HDR=YES;';"
Conn.Open sconnect
sSQLString = query ' query here
mrs.Open sSQLString, Conn
could you try changing the below
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
to
"Extended Properties='Excel 12.0 Xml;HDR=YES';"
if not advise what report_name is equal to.
alternatively try the below:
Dim sSQLString As String
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
Let DBPath = "" 'Path here
Let sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath & ";Extended Properties='text;HDR=YES;';"
Conn.Open sconnect
sSQLString = "" ' query here
mrs.Open sSQLString, Conn
I encountered a type mismatch error when passing a ADODB.Recordset object to a subroutine. See the code nugget below.
Sub CreateRS()
Dim oCONN As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim strSQL As String
Dim strConn As String
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ActiveWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes;"";"
Set oCONN = New ADODB.Connection
oCONN.Open strConn
strSQL = "SELECT * FROM mytable"
Set oRS = New ADODB.Recordset
oRS.Open strSQL, oCONN
' type mismatch error for the next statement
ProcessRS (oRS)
End Sub
Sub ProcessRS(ByRef RS As ADODB.Recordset)
....
End Sub
Did quite a bit of searches, yet didn't find a correct answer. Can anyone point out what is the right syntax to use here? Thanks!
This is the code I use to open a connection to an access database from excel. It used to work for more than a year.
Set dbname = New ADODB.Connection
theconnection = "//xxx.sharepoint.com/sites" & Application.PathSeparator & TARGET_DB
With dbname
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open theconnection
End With
By trial an error I've come to the conclusion that this line is causing the problem.
Set dbname= New ADODB.Connection
The problem began after an automatic update of my PC
My Excel version 2016 MSO (16.0.7726.1036) 32-bit
Please let me know if you have run also into this problem, and if you know any fix or workaround.
try to uncheck your 'ActiveX Data Objects' references and add them back:
Tools - References
or
use object to define a database:
Dim dbname As Object
Set dbname = CreateObject("ADODB.Connection")
or
if you create connection variable like this:
Dim con as New ADODB.Connection
change it to:
Dim con as ADODB.Connection
Set con = New ADODB.Connection
Maybe
Dim dbname As Object
Set dbname = CreateObject("ADODB.Connection")
theconnection = "//xxx.sharepoint.com/sites" & Application.PathSeparator & TARGET_DB
With dbname
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open theconnection
End With
I used like this , all code
Dim Rs As Object
Dim strConn As String
Dim i As Integer
Dim strSQL As String
strSQL = "select * from [table] "
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";" & _
"Extended Properties=Excel 12.0;"
Set Rs = CreateObject("ADODB.Recordset")
Rs.Open strSQL, strConn
If Not Rs.EOF Then
With Ws
.Range("a1").CurrentRegion.ClearContents
For i = 0 To Rs.Fields.Count - 1
.Cells(1, i + 1).Value = Rs.Fields(i).Name
Next
.Range("a" & 2).CopyFromRecordset Rs
End With
End If
Rs.Close
Set Rs = Nothing
I'm trying to open a CSV file and query it and return the results into column A of the second worksheet of "ThisWorkbook".
I'm not getting any errors so I do not see why it is not copying the record set into excel.
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim currentDataFilePath As String
Dim currentDataFileName As String
Dim nextRow As Integer
currentDataFilePath = "C:\Users\M\folder\"
currentDataFileName = "csv-file"
con.Open "Provider=Microsoft.JET.OLEDB.4.0;" & _
"Data Source=" & currentDataFilePath & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited;IMEX=1"""
'rs.ActiveConnection = con
rs.Open "SELECT Name FROM [" & currentDataFileName & ".csv] WHERE Datatype ='TYPE3'",
con
ThisWorkbook.Worksheets("Sheet2").Range("A:A").CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
End Sub
You might refer to the CopyFromRecordset() method.
Based on your code above, after the rs.Open command you would add something like this:
ActiveWorksheet.Range("A1").CopyFromRecordset rs
See more here: http://msdn.microsoft.com/en-us/library/office/ff839240%28v=office.15%29.aspx