0

I was trying to write a VBA to run a query assigned to MySQL only when the HCR_DM.HCR_DM_FACT table is fully loaded. I'm using the count of distinct source in that table to decide if it is fully loaded.

When I was running the Macro below, I got an error message for the Do While line, saying that Object doesn't support this property or method.

I'm quite new to VBA, and I couldn't figure out what need to be adjusted. Can some one help me with this?

Thanks!

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
1

1 Answer 1

1

You're not selecting a field. The lines

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

Should probably be

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

Also, note the typo in that you declared rs but you're filling rf with the Recordset. I recommend you use the Option Explicit statement to help find these. You can read more about it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.