【发布时间】:2014-12-17 10:05:26
【问题描述】:
我试图编写一个 VBA 以仅在 HCR_DM.HCR_DM_FACT 表完全加载时运行分配给 MySQL 的查询。我正在使用该表中不同源的计数来确定它是否已完全加载。
当我运行下面的宏时,我收到了 Do While 行的错误消息,说 Object doesn't support this property or method.
我对 VBA 很陌生,我不知道需要调整什么。有人可以帮我解决这个问题吗?
谢谢!
Const CNSTR = "Provider = OraOLEDB.Oracle; Data Source =CSDPRO; ODBC;DRIVER={Oracle ODBC Driver};SERVER=CSDPRO;User ID=HCR_SANDBOX;password=******"
Sub FillWithSQLData(strSQL As String, wkSht As String)
' Given a SQL query and worksheet, fills the worksheet with a data dump of the SQL query results
' Define variables
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql_count As String
' Set variables
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
' Connect to SQL Server
With cn
.ConnectionString = CNSTR
.Open
End With
' Query SQL Server and return results as a data dump in cell A2 of the specified worksheet
With rs
.ActiveConnection = cn
sql_count = "select count( distinct a.src_table) from hcr_dm.hcr_dm_fact a"
Set rf = cn.Execute(sql_count)
Do While rf.Fields.Value = 8
.Open strSQL
Loop
Worksheets(wkSht).Range("A2").CopyFromRecordset rs
.Close
End With
' Close connection
cn.Close
Set rs = Nothing
Set Conn = Nothing
End Sub
Sub Refresh()
' Define SQL query
Dim mySQL As String
mySQL = "select a.oracle_fin_company_id || ' - ' || a.oracle_fin_company_desc as COMPANY " & _
"From hcr_dm.legal_entity_summary a " & _
"where a.Company_Header = 'Filed'"
' Choose worksheet where results should be displayed
Dim myWkSht As String
myWkSht = "Sheet1"
' Call connection sub
Call FillWithSQLData(mySQL, myWkSht)
End Sub
【问题讨论】:
-
看起来有点乱。您在谈论 MySQL,在您的 ConnectionString 中使用
OraOLEDB.Oracle和DRIVER={Oracle ODBC Driver}。对于正确的 ConnectionString,请检查 ConnectionStrings.com 或 Connection Strings