I have setup some queries in MS access that fetch data from a MS SQL server on the network. The queries work fine when I run them from MS access. However, when trigger the same query through VBA in Excel (need of my dashboard), then it says ODBC connection failed. I am not able to figure out what is broken in the process. All details below:
ODBC connection string in MS Access - set in the properties of the query
ODBC;DRIVER=SQL Server;SERVER=xyz;UID=xyz_reader;PWD=xyzpassword;Network=DBMSSOCN;Address=xyz,1433
Connection string used in Excel VBA - this is used to trigger the odbc query in ms access
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & mydb.accdb
Error that I get
Run-time error '-2147467259 (80004005)':
ODBC--connection to 'SQL Serverxyz' failed.
Excel VBA Connection is an ADODB connection so you need to add a references Tool>>References then add “Microsoft ActiveX Data Object Library”
Then use code like
Dim strConn As ADODB.Connection
Dim rs As ADODB.Recordset
Set strConn = New ADODB.Connection
Set rs = New ADODB.Recordset
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=YourDATABASE;Data Source=YourServer"
strSQL = "Your Select statement”
strConn.Open
rs.Open strSQL, strConn
Now you have your values in record set. I hope this will solve your issue.
Related
Recently my company just performed a server migration and now one of my Excel VBA is not working,
Here's my code snippet:
Dim strSQL As String, conStr As String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
'On Error GoTo exitsub
Path = ActiveWorkbook.Sheets("Options").Cells(1, 6)
conStr = "Provider=Microsoft.ace.OLEDB.12.0;Data Source='" & Path & "';Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';"
strSQL = "SELECT * From [MASTER]"
cnn.Open ConnectionString:=conStr 'stuck at this line
rs.Open strSQL, cnn, adOpenDynamic, adLockReadOnly
The place where I'm stuck at is when its trying to perform the SELECT query. The macro triggers when I save the file but doing so only give me this error:
I've checked my reference for Microsoft ActiveX Data Object, and confirmed that the latest one I have (16.0) is selected.
How do I fix this?
Problem
Server-side automation of office is not supported https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2
Possible workaround
Do it with .Net https://stackoverflow.com/a/52050257/495455
Possible solution/workaround
https://knowledge.informatica.com/s/article/107777?language=en_US
Solution
This is as designed behavior from Microsoft for the Excel driver which currently does not have feature to read from a password protected Excel file, unless it is open.
Workaround
Open the Excel file whose definition is being imported and provide the correct password and try to import the definition from Designer.
I am trying to connect to Oracle Database, using Excel VBA, but not able to connect. Any guidance is welcome.
Sub connection()
Dim cn As ADODB.connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.connection
Set rs = New ADODB.Recordset
constr = "Driver={Microsoft ODBC for Oracle}; CONNECTSTRING=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<HostName>)(PORT=<port>))(CONNECT_DATA=(SID=<sid>))); uid=<UserName>; pwd=<Password>;"
cn.Open (constr)
End Sub
When trying to debug, it's giving the below error at Open Connection.
Automation Error
Unspecified Error
Error While Creating the Database connection
I need to get data from Access to excel vba, I use ADODB.
My problem is that although the database is relatively small and query results 30-40 records only, the process gets stuck either with the ".open" or with "copyfromrecordset" line and takes 40-50 secs to display the records.
This is my code.
I made some tests with different cursor types and locktypes with no result. The query is working executed directly from access and I have no issue when the connection points locally to my PC. I am on office 365. I referenced the activex data objects 2.8 library.
Sub loadTestDisplay2()
Dim myConnectiom As ADODB.Connection
Dim myRS As ADODB.Recordset
Const conStringNet As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\ourserver-f04\COE-Shared\Data Tool\Access\adsLoadTest.accdb; Persist Security Info=False;"
Dim sql As String
sql = "SELECT * FROM tblLoad where user is Null"
Set myConnection = New ADODB.Connection
Set myRS = New ADODB.Recordset
myConnection.ConnectionString = conStringNet
myConnection.Open
With myRS
.ActiveConnection = conStringNet
.Source = sql
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
Sheets.Add
Range("A2").CopyFromRecordset myRS
myRS.Close
myConnection.Close
End Sub
I'm developing an Excel VBA application using ADO. I'm trying to open a recordset, but the open method fails if my table has more than 65536 lines. I know this number is the old Excel line limit, but I'm using the 2016 version and the correct connection strings. Perhaps it's some library referenced in my project, but I can't find out which one.
I would appreciate very much if I could get some help with this.
The error:
Runtime error '-2147217865 (80040e37)': The Microsoft Jet database engine could not find the object 'My sheet$A8:AD70000'. Make sure the object exists and that you spell its name and the path name correctly.
My code (I had to switch the worksheet name so I don't reveal any sensitive data):
Sub MySub()
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Set rs = New ADODB.Recordset
Set conn = New ADODB.Connection
str = "SELECT * FROM [My sheet$A8:AD70000];"
'Opening connection with the workbook
conn.ConnectionTimeout = 90
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Application.ThisWorkbook.FullName & ";" & _
"Extended Properties=""Excel 12.0 Macro;HDR=YES;IMEX=1"";"
conn.Open
rs.Open str, conn, adOpenForwardOnly, adLockReadOnly, adCmdText
rs.Close
conn.Close
End Sub
If I change "My sheet$A8:AD70000" to "My sheet$A8:AD60000", this code Works.
The libraries I'm using:
Visual Basic For Applications
Microsoft Excel 16.0 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Office 16.0 Object Library
Microsoft ActiveX Data Objects 6.1 Library
OLE Automation
Thank you very much for your attention.
I'm having a problem during one week, and I couldn't find the solution. I'm trying to read a file in xlsx on vb6 as a component of the asp classic.
First issue that I couldn't be able to solve was this
Dim cn As New ADODB.Connection
Dim provider As String
provider = "Driver={Microsoft Excel Driver (*.xlsx)};DBQ=" & nomArq & ";"
Set cn = CreateObject("ADODB.Connection")
cn.Open provider
The error that I got in this connection was
"[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"
The second try was in this way
Dim cn As New ADODB.Connection
'Dim provider As String
With cn
.provider = "Microsoft.ACE.OLEDB.12.0;Data Source=" & nomArq & ";" & ";Extended Properties=\""Excel 8.0;HDR=Yes\;"""
.Open
End With
And the erros was
"Format of the initialization string does not conform to the OLE DB specification."
I also tried in another way and I got the error:
"could not find installable isam"
but I don't have the code hear anymore.
someone could help me, I looked in all most everything on google
I don't really understand what you are trying to do but it seems like you are trying to establish a connection (looking at your code).....try something like this with what you are doing....
Dim g_cnDB as ADODB.Connection
Set g_cnDB as ADODB.Connection
g_cnDB.ConnectionString = "Provider=TEXT;User ID=TEXT; Pwd=TEXT; Initial Catalog=TEXT; Data Source=TEXT"
'Then set it's timeout to be unlimited, so your connection remains open until you close it
g_cnDB.CommandTimeout = 0
'Establishing the connection
g_cnDB.Open