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
Related
I'm using Excel VBA code to connect to a CSV file (24,179,689 rows) via ADODB connection. The macro runs and gets data from the excel with a specific filter on one column. With this filter that I'm trying now, it should return about 1500 rows of data.
I have checked this by loading the CSV manually somewhere else, and the data is actually there. But when I use the ADODB connection, my recordset remains empty.
I did some extra test: count(*) on the complete CSV file, and there I see the error: it only returns 155,535 rows. So probably the specific filter that I'm applying is not in that data and therefore it returns 0 rows.
This is my code:
Public adoConn As ADODB.Connection
Public adoRS As ADODB.Recordset
Sub getdata()
Set adoConn = New ADODB.Connection
Set adoRS = New ADODB.Recordset
Dim rawFile As String
Dim strSQL As String
'The xlsx file to treat as a database
rawFile = "myPathName"
'Open a connection to the workbook
sconnect = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" & rawFile & ";Extended Properties='text;HDR=YES;FMT=Delimited'"
'Write the SQL necessary to get the data you want
sql2 = "SELECT count(*) from [MyFileName.csv]"
'Now we open up our recordset using the connection and the sql statement
adoRS.Open sql2, adoConn, adOpenStatic
Debug.Print (adoRS.EOF)
'Last, we dump the results in this viz sheet
Blad1.Range("A1").CopyFromRecordset adoRS
adoRS.Close
adoConn.Close
End Sub
So then it returns the 155,535.
I also tried by creating a ADODB command and not using the connection as above. Or with connection timeouts. No results.
Is this a memory issue or something else? How can it be solved?
Please try this and see if it does what you want. Also, set a Reference to Microsoft ActiveX Data Objects 2.8 Library.
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = "C:\your_path_here\"
sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath & ";Extended Properties='text;HDR=YES;FMT=Delimited'"
Conn.Open sconnect
sSQLSting = "SELECT * From CSV1.csv WHERE ID = 2"
mrs.Open sSQLSting, Conn
ActiveSheet.Range("A2").CopyFromRecordset mrs
'Close Recordset
mrs.Close
Conn.Close
My CSV looks like this.
I have a little over a million rows in my CSV; I can't put in 23 million rows! Anyway, in my test, with just over a million rows, I got the exact results that I expected, in about 1 second, and my computer is super-super-super-slow!!
I am developing an Excel VBA program that I want to connect to a remote Oracle 11g database, run a query, and return the data to Excel.
Connection strings and drivers are all OK as far as I know. (see below)
It was working perfectly for a while yesterday, connecting to db and returning with correct data. Then I saved and closed the workbook, opened it 20 minutes later (no changes made!) and when I ran the macro I got the following error:
Runtime error '-2147418113 (8000ffff)': Catastrophic failure
any ideas what could be causing the error?
Could it be something on the DB's side?
Thanks
Sub ExtractFromOracle(environment As String)
Dim cn As ADODB.connection
Dim recordSet As ADODB.recordSet
Set cn = CreateObject("ADODB.Connection")
Set recordSet = CreateObject("ADODB.recordset")
Dim SQLQuery As String
SQLQuery = "SELECT User_Id, Prof_Id FROM user_profile ORDER BY User_Id ASC"
Dim returnData As Variant
Dim returnedRowsCount As Integer
Dim connectionString As String
connectionString = ReturnConnectionString(environment)
cn.Open (connectionString) ------------> ERROR OCCURS HERE
Set recordSet = cn.Execute(SQLQuery)
returnData = Application.Transpose(recordSet.GetRows)
returnedRowsCount = UBound(returnData)
If Not SheetExist(environment) Then
CreateTab (environment)
End If
Worksheets(environment).Activate
ActiveSheet.Range("A1:B" & returnedRowsCount) = returnData
Set rs = Nothing
Set cn = Nothing
ConsolidateUsers (environment)
End Sub
CONNECTION STRING :
"Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxx)(PORT=xxxx)))(CONNECT_DATA=(SID=xxxxx)(SERVER=DEDICATED)));User Id=xxxxxx;Password=xxxxxxx;"
Can you try to access the ADODB as below
Dim strSQL As String, conStr as String
Dim cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
conStr = "Provider=OraOLEDB.Oracle;Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxx)(PORT=xxxx)))(CONNECT_DATA=(SID=xxxxx)(SERVER=DEDICATED)));User Id=xxxxxx;Password=xxxxxxx;"
Dim SQLQuery As String
SQLQuery = "SELECT User_Id, Prof_Id FROM user_profile ORDER BY User_Id ASC"
cnn.open conStr
rs.Open SQLQuery, cnn, adOpenStatic, adLockOptimistic, adCmdText
I fixed the error so I though I would update for anyone with the same issue who finds this thread.
It turns out that the 'Catastrophic Failure' error was due to my Oracle ODBC driver (oraOLEDB.oracle) becoming somehow corrupted, I couldn't even reinstall it properly, the error only occurred on my machine.
In the end I did a system restore to a point from before the issue and all was fixed. Not the most elegant solution but it worked.
Let me start of by saying I am new to both this site as well as VBA so please bear with me. Thank you in advance for your help.
I have a VBA function that runs an existing query from Access. Some of the tables being queried are stored in an Oracle database that require a user specific password access. Right now, a sub that I wrote to automate a report calls this function 7 times and requires the user to input their Oracle password each time the function is called (it also stops the sub and gives an error message if they type in the password incorrectly which I see as a likely event if they need to do it 7 times). The code works but I would like to find a way to have the code ask for the password once and be done with it. All of the solutions I have found involve connecting to and querying Oracle directly which requires very complicated SQL coding that I am by no means capable of writing.
I am also having an issue where the columns show up in the excel sheet in a different order than they do in Access for some of the queries. This seems to be consistent so it isn't to big of a problem but I would like to know how to prevent this to prevent any future issues.
Here is the code for the function I am currently using. Any insight would be greatly appreciated!
Option Explicit
'Single Argument "qryName" as string. Runs access qry and copys recordset to active sheet.
Function AccessQueryPull(qryName As String)
'Declare variables
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
'open the connection to the access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=filePath.accdb;"
'format the command to run the query
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = qryName
cmd.ActiveConnection = cn
'execute the query
Set rs = New ADODB.Recordset
Set rs = cmd.Execute()
'copy data to excel sheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rs
'Cleanup
rs.Close
Set rs = Nothing
The reason you are prompted each time for Oracle credentials is that you create a fresh, new connection to MS Access each time the function is called.
Simply persist the MS Access connection by connecting once in the Sub that calls function and pass connection object as parameter. In this way, any connection error is caught in the parent routine. As for column order simply declare the columns in the needed order in SQL statement.
Sub RunQueries()
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
'open the connection to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=filePath.accdb;"
Call AccessQueryPull(cn, "SELECT Col1, Col2, Col3 FROM Qry1") ' AND OTHER 6 CALLS
cn.Close
Set cn = Nothing
End Sub
Function AccessQueryPull(accConn As ADODB.Connection, qryName As String)
'format the command to run the query
Dim cmd As New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.CommandText = qryName
cmd.ActiveConnection = accConn
'execute the query
Set rs = New ADODB.Recordset
Set rs = cmd.Execute()
'copy data to excel sheet
ActiveSheet.Cells(2, 1).CopyFromRecordset rs
'Cleanup
rs.Close
Set rs = Nothing: Set cmd = Nothing
End Function
I'm trying to do a query in an access db and then get this query result and insert into a Excel sheet, but nothing I do work. This is my code
Sub Analise()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sSQL As String
Sheets("Plan1").Range("A2:A23").Select
For Each Regra In Selection
If Regra = "MYRULE" Then
sSQL = "SELECT ITEM !, ITEM2 FROM base WHERE NomeRegra = ""'MyValue'"" "
MyConn = "MyPath\base.mdb"
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, CursorType:=AdForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText
Sheets("Plan1").Range("D1:E1") = Array("Field1", "Field2")
Range("D2:E100000").CopyFromRecordset rst
rst.Close
cnn.Close
End If
Next Regra
Have you tried using MS Query in Excel using your Access file as the DB? From the data tab in Excel,
'Get External Data' 'From Other Sources' dropdown and then 'From MS Query'
Select MS Access Database and then find your file. When the Query Wizard pop up appears, hit cancel and then 'Yes' to continue editing in MS Query. If you'd rather write the SQL yourself, you can press the sql button, but the language is a little different than Access. It might take some playing around, but MS query can be a very powerful tool.
I want to load specific data(query) from Oracle database into excel. I am able to achieve it through external connections, (Mircosoft Data Access - OLE DB Provider for Oracle), but all of the table is loaded. This was by hit and try. I am not aware what OLE DB is.
Is it possible to load specific data using that method.
How can I load the same from VBA, I have read many sources, blogs but none are lucid and comprehensive. Can somebody please explain for a newbie. Or refer to me some book/source.
This function will connect to an Oracle Database using ADODB. Make sure to include Microsoft ActiveX Data Objects 2.8 as a reference. You can configure the connectingstring to suit your needs if there are admin privileges.
It will store your database into a variant.
Function ConToDataBase(DBPath As String) As Variant
Dim Con As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim SQL As String
SQL = "SELECT * FROM TableName"
Set Con = New ADODB.Connection
With Con
.ConnectionString = "Provider=OraOLEDB.Oracle;Data source=" & DBPath & ";UserID=;Password:=;"
.Open
End With
Set Rs = New ADODB.Recordset
Rs.Open SQL, Con
Dim Var As Variant
Var = Rs.GetRows
ConToDataBase = Var
Set Rs = Nothing
Con.Close
End Function