【问题标题】:VBA LOOP in ORACLE SQLORACLE SQL 中的 VBA 循环
【发布时间】: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.OracleDRIVER={Oracle ODBC Driver}。对于正确的 ConnectionString,请检查 ConnectionStrings.comConnection Strings

标签: sql oracle vba loops


【解决方案1】:

您没有选择字段。线条

Set rf = cn.Execute(sql_count)
Do While rf.Fields.Value = 8

应该是

Set rs = cn.Execute(sql_count)
Do While rs.Fields(0).Value = 8

另外,请注意您声明 rs 时的拼写错误,但您使用 Recordset 填充 rf。 我建议您使用Option Explicit 语句来帮助找到这些。你可以阅读更多关于它的信息here

【讨论】:

    猜你喜欢
    • 2019-05-20
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多