+ Reply to Thread
Results 1 to 1 of 1

VBA and SQL:kicks out data in predicable cells

Hybrid View

  1. #1
    Registered User
    Join Date
    04-05-2006
    Posts
    2

    VBA and SQL:kicks out data in predicable cells

    I am trying to write a macro that queries a MS SQL server and kicks out data in predicable cells -- say start printing results at A10, but I want to print certain headings above each column as well. AND perhaps do some calculations and print the results to other worksheets ...

    here is what I have:


    Sub GetTrans()

    Dim i As Integer
    Dim iNumCols As Integer, iNumRows As Integer


    On Error GoTo ErrHandler

    Sheets("Trans").Select
    Range("A10").Select

    If (Range("A10").Value <> vbNullString) Then
    iNumRows = Sheets("FuturesTrans").Range("A10").End(xlDown).Row
    iNumCols = Sheets("FuturesTrans").Range("A10").End(xlToRight).Column
    Sheets("Trans").Range(Cells(1, 1), Cells(iNumRows, iNumCols)).Select
    Selection.ClearContents
    End If

    For i = 1 To ActiveSheet.QueryTables.Count
    ActiveSheet.QueryTables(i).Delete
    Next

    With ActiveSheet.QueryTables.Add(Connection:= _
    "ODBC;DRIVER=SQL Server;SERVER=SQL\SQL;UID=User;PWD=password;APP=Microsoft® Query;" _
    , Destination:=Range("A10"))
    .CommandText = Array( _
    "SELECT statement here"
    )
    .Name = "Transactions Query"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = True
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
    End With


    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbCritical, "Error: Get Transactions"
    End Sub






    thanks.
    Last edited by sep27; 04-05-2006 at 04:43 PM.

+ Reply to Thread

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