Results 1 to 2 of 2

Macro for running the SQL multiple times

Threaded View

sawoodalam1989 Macro for running the SQL... 02-27-2013, 05:18 AM
nathansav Re: Macro for running the SQL... 02-27-2013, 07:00 AM
  1. #1
    Registered User
    Join Date
    01-26-2013
    Location
    chennai
    MS-Off Ver
    Excel 2007
    Posts
    20

    Macro for running the SQL multiple times

    Currently the SQL will pick the value which is present in "Sheet2.Range("C2")" and execute the SQL and store the output into Selection.Offset(1, 1) = sourceRst.Fields("DT_KEY") .
    My requirement is i will be having values in Sheet2.C3, C4, C5,and goes on. So, the SQL should pick the value 1 by 1 and execute it and populate the data into the corresponding DB Output field.

    Kindly provide me the code which will make the SQL run 1 by 1 till it finds a NULL value in the Sheet2.C field.

    I am attaching the Sheet below, kindly look into Sheet2.B column and Sheet2.C column.
    Sheet2.C is the date which is passed into the SQL and Sheet2.B is the output from Database.

    Sub Ora_Connection()
    
    Dim sourceCnn As ADODB.Connection
    Dim sourceRst As ADODB.Recordset
    
    Set sourceCnn = New ADODB.Connection
    Set sourceRst = New ADODB.Recordset
    
    strCon = "Provider=OraOLEDB.Oracle;User ID=;Password=;Data Source="
      
    sourceCnn.Open (strCon)
    sourceRst.ActiveConnection = sourceCnn
    
    
    sourceRst.Source = "select DT_KEY from dt_dim where CAL_DT = TO_DATE('" & Sheet2.Range("C2").Value & "', 'MM/DD/YYYY')"
    sourceRst.Open
    
    Sheets("Sheet2").Select
    
       Cells(lastrow, 1).Select
       Do While Not sourceRst.EOF
        Selection.Offset(1, 1) = sourceRst.Fields("DT_KEY")
      Selection.Offset(1, 0).Select
                       sourceRst.MoveNext
              Loop
      sourceRst.Close
    
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1