Results 1 to 9 of 9

Thread: Execute SQL From Excel (VBA)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Mechanic's Avatar
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Lightbulb Execute SQL From Excel (VBA)

    Hi,

    This can be used to Fetch(to sheet/control)/Modify/Excute SQL's

    -Requires reference to Microsoft ActiveX Data Objects 2.8 Library
    (I know a lot of would say can be done with Late binding but i perfer this)

    Code:
    Function SQLJuicer(strSQLString As String, strDataBaseAddress As String, Optional rngWhereToPasteRange As Range, Optional blnReturnListArrayInstead As Boolean = True) As Variant
     
        Dim adoConnection   As New ADODB.Connection
        Dim adoRcdSource    As New ADODB.Recordset
        Dim strDBPath As String
     
        On Error GoTo Errs:
        strDBPath = strDataBaseAddress
     
        adoConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDBPath
     
        If UCase(Left(strSQLString, 6)) = "SELECT" Then
            adoRcdSource.Open strSQLString, adoConnection, 3
     
            If rngWhereToPasteRange Is Nothing And blnReturnListArrayInstead = True Then
                If (adoRcdSource.BOF Or adoRcdSource.EOF) = False Then
                    SQLJuicer = adoRcdSource.GetRows
                End If
            ElseIf Not rngWhereToPasteRange And blnReturnListArrayInstead = False Then
                With rngWhereToPasteRange
                    .ClearContents
                    .Cells(1).CopyFromRecordset adoRcdSource
                    adoRcdSource.Close
                End With
            End If
        Else
            adoConnection.Execute strSQLString
        End If
     
        GoTo NormalExit
    Errs:
        MsgBox Err.Description, vbCritical, "Error!"
        Err.Clear: On Error GoTo 0: On Error GoTo -1
     
    NormalExit:
        Set adoConnection = Nothing
        Set adoRcdSource = Nothing
        strDBPath = vbNullString
     
    End Function

    To Load a List to List Box/Combo Box:
    Code:
     
    Me.ComboBox1.List = Application.Transpose(SQLJuicer("SELECT Name FROM Employee", "C:\Mydatabase.mdb", ,True))
    To Execute A SQL:
    Code:
     
    Call SQLJuicer(YourSQLString,"C:\Mydatabase.mdb")
    And to get data to a Range:
    Code:
     
    Call SQLJuicer(YourSQLString,"C:\Mydatabase.mdb",Worksheets("Sheet1").Range("A1"))
    Last edited by Mechanic; 05-18-2011 at 02:01 PM. Reason: examples added
    Mechanic!
    ------------------------------------------------------------------------------------------------------------------------
    //Caffeine is the only way to make my brain run in single-threaded mode. //

Similar Threads

  1. SQL output from Excel VBA macro
    By goldenbutter in forum Excel Help
    Replies: 3
    Last Post: 05-07-2013, 08:07 PM
  2. Upload Excel Data to SQL Table
    By littleiitin in forum Excel and VBA Tips and Tricks
    Replies: 3
    Last Post: 08-22-2012, 11:02 AM
  3. SQL in Excel
    By Nishant Choudhary in forum Excel Help
    Replies: 2
    Last Post: 01-24-2012, 05:55 AM
  4. Replies: 2
    Last Post: 11-17-2011, 07:49 PM
  5. Execute SQL From Excel (VBA)
    By Mechanic in forum Excel Help
    Replies: 0
    Last Post: 05-13-2011, 10:27 AM

Tags for this Thread

Posting Permissions

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