Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sylvain Le Courtois

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Excel : Make a query on a Oracle database and return the result (useful for sheet formulas)

// This should be pasted in a module of the workbook
Function ORAQUERY(strHost As String, strDatabase As String, strSQL As String, strUser As String, strPassword As String)
  Dim strConOracle, oConOracle, oRsOracle
  Dim StrResult As String
  
  StrResult = ""
  
  strConOracle = "Driver={Microsoft ODBC for Oracle}; " & _
         "CONNECTSTRING=(DESCRIPTION=" & _
         "(ADDRESS=(PROTOCOL=TCP)" & _
         "(HOST=" & strHost & ")(PORT=1521))" & _
         "(CONNECT_DATA=(SERVICE_NAME=" & strDatabase & "))); uid=" & strUser & " ;pwd=" & strPassword & ";"
  Set oConOracle = CreateObject("ADODB.Connection")
  Set oRsOracle = CreateObject("ADODB.Recordset")
  oConOracle.Open strConOracle
  Set oRsOracle = oConOracle.Execute(strSQL)
  Do While Not oRsOracle.EOF
      If StrResult <> "" Then
        StrResult = StrResult & Chr(10) & oRsOracle.Fields(0).Value
      Else
        StrResult = oRsOracle.Fields(0).Value
      End If
    oRsOracle.MoveNext
  Loop
  oConOracle.Close
  Set oRsOracle = Nothing
  Set oConOracle = Nothing
  ORAQUERY = StrResult
End Function

VBA procedure to pen an Excel workbook and refresh all datas in the QueryTables and PivotTable objects

// description of your code here
// Can also use the Workbook Open event ( Private Sub Workbook_Open() )

Sub Auto_Open()
Application.DisplayAlerts = False
    ChDir "T:\EXPLOIT\TSMENV\EXCEL\politiques"
    Workbooks.Open Filename:="t:\EXPLOIT\TSMENV\EXCEL\politiques\politiques.xls", _
        UpdateLinks:=3
    For i = 1 To ActiveWorkbook.PivotCaches.Count
        ActiveWorkbook.PivotCaches(i).RefreshOnFileOpen = False
    Next
    For i = 1 To ActiveWorkbook.Sheets.Count
        For j = 1 To ActiveWorkbook.Sheets(i).QueryTables.Count
            ActiveWorkbook.Sheets(i).QueryTables(j).RefreshOnFileOpen = False
        Next
    Next
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.Save
    ActiveWindow.Close
    Application.Quit
End Sub
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS