Hi Everybody,
I have written a macro which pulls data from a database in to an Excel sheet and then performs certain actions on it. The SQL query that I have used to pull the data uses date parameters. I have to edit the date parameters in the macro every time I run the query. Can someone please advise, how can I tweak the macro to prompt for date input, every time it is run?
The SQL script in the macro is as below. I have colored the parts red, where the date input is required.
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = "Data"
Sheets("Data").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=XXXXXXXXXX;DFQ=\\BACKUPSERVER\ABC\RST\XYZ.QBW;SERVER=QODBC;Optim" _
), Array( _
"izerDBFolder=%UserProfile%\QODBC Driver for QuickBooks\Optimizer;OptimizerAllowDirtyReads=N;SyncFromOtherTables=Y;ForceSDKVersi" _
), Array("on=;IAppReadOnly=Y")), Destination:=Range("$A$1")). _
QueryTable
.CommandText = Array( _
"SELECT InvoiceLine.TxnDate, InvoiceLine.RefNumber, InvoiceLine.SalesRepRefFullName, InvoiceLine.CustomerRefFullName, InvoiceLine.InvoiceLineItemRefFullName, InvoiceLine.InvoiceLineDesc, InvoiceLine.In" _
, _
"voiceLineQuantity, InvoiceLine.InvoiceLineRate, InvoiceLine.InvoiceLineAmount, InvoiceLine.CustomFieldInvoiceLineCommission" & Chr(13) & "" & Chr(10) & "FROM InvoiceLine InvoiceLine" & Chr(13) & "" & Chr(10) & "WHERE (InvoiceLine.TxnDate>={d '2014-03-28'} " _
, _
"And InvoiceLine.TxnDate<={d '2014-04-03'})" & Chr(13) & "" & Chr(10) & "ORDER BY InvoiceLine.SalesRepRefFullName, InvoiceLine.TxnDate, InvoiceLine.CustomerRefFullName" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_XYZ"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("Table_Query_from_XYZ").Unlink
Dim oSh1 As Worksheet
Set oSh1 = ActiveSheet
'remove table or list style
oSh1.ListObjects("Table_Query_from_XYZ").Unlist
I would highly appreciate if you someone can help !!!!
Thanks... Murtaza
Bookmarks