简体   繁体   English

链接到Oracle的Excel VBA执行访问查询

[英]Excel VBA Executing Access Query that Links to Oracle

Let me start of by saying I am new to both this site as well as VBA so please bear with me. 首先,我要说我对这个网站和VBA都是陌生的,所以请多多包涵。 Thank you in advance for your help. 预先感谢您的帮助。

I have a VBA function that runs an existing query from Access. 我有一个VBA函数,可以从Access运行现有查询。 Some of the tables being queried are stored in an Oracle database that require a user specific password access. 一些要查询的表存储在需要用户特定密码访问权限的Oracle数据库中。 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). 现在,我编写的用于自动生成报表的子程序调用了该函数7次,并且每次调用该函数时都要求用户输入Oracle密码(如果输入了错误的密码,该子程序也会停止该子程序并给出错误消息如果他们需要做7次,我认为这是一个可能的事件)。 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. 我发现的所有解决方案都涉及直接连接和查询Oracle,这需要非常复杂的SQL编码,而我却根本无法编写。

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. 我还遇到一个问题,即某些查询中的列以与Access中不同的顺序显示在excel工作表中。 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. 每次都会提示您输入Oracle凭据的原因是,每次调用该函数时,都会创建一个与MS Access的全新连接。

Simply persist the MS Access connection by connecting once in the Sub that calls function and pass connection object as parameter. 只需在调用函数的Sub中进行一次连接,然后将连接对象作为参数进行传递,即可持久保持MS Access连接。 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. 至于列顺序,只需在SQL语句中按所需顺序声明列即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM