Solved

Convert SQL script to Excel VBA module

Posted on 2008-06-19
2
1,380 Views
Last Modified: 2013-12-19
What is the general method to incorporate an SQL script (.sql file) into an Excel VBA module? At this point, I have to run the SQL script using SQL*XL to get the data into an Excel worksheet. Then I run an Excel VBA script to get the end result. Both these actions work well independently; however, I want a single VBA module that includes the SQL script and the existing VBA script so it all runs as one module. I can make the connection to the Oracle database independently so I don't need that part. Once my connection is established, though, I want to run the combined SQL/VBA module to do what the SQL script and the VBA code now do independently. How should I go about doing this?
0
Comment
Question by:glennes
2 Comments
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 21828572
If the script does not return a recordset then this should work:

   Dim MyDatabase As ADODB.Connection
   Dim FilePath As String
   Dim FileNumber As Long
   Dim FileData As String

   FilePath = "C:\Full\Path\To\File.sql"
   FileNumber = FreeFile
   Open FilePath For Binary Access Read As FileNumber
   ' Use following if non-unicode data
   FileData = StrConv(InputB(LOF(FileNumber), FileNumber), vbUnicode)
   ' Use following if unicode data
   'FileData = InputB(LOF(FileNumber), FileNumber)
   Close FileNumber
   
   ' Open database connection
   Set MyDatabase = New ADODB.Connection
   MyDatabase.CursorLocation = adUseClient
   MyDatabase.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\full\path\to\database.mdb'; User Id=admin; Password=;"
   ' For more information about Open syntax:
   '   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
   '   and navigate to Open Method (ADO Connection)
   '   Additional help constructing connection strings can be found at http://www.connectionstrings.com/

   ' Execute any SQL statement or script
   MyDatabase.Execute FileData

If it does return a recordset then:

   Dim MyDatabase As ADODB.Connection
   Dim MyRecordset As ADODB.RecordSet
   Dim FilePath As String
   Dim FileNumber As Long
   Dim FileData As String

   FilePath = "C:\Full\Path\To\File.sql"
   FileNumber = FreeFile
   Open FilePath For Binary Access Read As FileNumber
   ' Use following if non-unicode data
   FileData = StrConv(InputB(LOF(FileNumber), FileNumber), vbUnicode)
   ' Use following if unicode data
   'FileData = InputB(LOF(FileNumber), FileNumber)
   Close FileNumber
   
   ' Open database connection
   Set MyDatabase = New ADODB.Connection
   MyDatabase.CursorLocation = adUseClient
   MyDatabase.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='C:\full\path\to\database.mdb'; User Id=admin; Password=;"
   ' For more information about Open syntax:
   '   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadomethods.asp
   '   and navigate to Open Method (ADO Connection)
   '   Additional help constructing connection strings can be found at http://www.connectionstrings.com/

   ' Execute any SQL statement or script that returns a recordset
   Set MyRecordset = New ADODB.Recordset
   MyRecordset.Open FileData, MyDatabase, adOpenDynamic, adLockPessimistic

The above code assume the .sql file is just a text file containing the script.

Kevin
0
 

Expert Comment

by:kismokus13
ID: 21902167
Ok, here is a twist in teh problem,
suppose my sql script has a create #tmp statement in it first, and then the result is returned from querying that. For some reason the above solution does not work then.

If i read teh file and break it into 2 statements, one that creates the #tmp table and I call cmd.Execute with that, then I run your solution for the second part, that works...

IS there a more elegant way of doing this?

Thanks,
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question