how do i store query results into variables and then print those variables to specific cells in an excel worksheet?
I am trying to query a database using VBA and then manipulate the data and place it in specific places ...
thanks.
Printable View
how do i store query results into variables and then print those variables to specific cells in an excel worksheet?
I am trying to query a database using VBA and then manipulate the data and place it in specific places ...
thanks.
Welcome to the forums :wave:
Can you post the code that you have so far.
thanks for the welcome.
this is what i have started. Thanks for the help.
Sub GetTrans()
Dim i As Integer
Dim iNumCols As Integer, iNumRows As Integer
Dim begin_date As Variant
begin_date = Sheets("JPY").Range("AL12").Value
end_date = Sheets("JPY").Range("AL13").Value
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("FuturesTrans").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=pass;APP=Microsoft® Query;" _
, Destination:=Range("A10"))
.CommandText = Array( _
"SELECT statement" _
)
.Name = "Futures 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 Futures Transactions"
End Sub
No ideas?
I'm still not clear what you need to do. The above looks fine. and it gives you a table of data. What do you now need to do with the resulting table?