I am running the below code:
[vba]
Public Sub SQLImport()
Dim Begin As Date
Dim Finish As Date
Dim databaseName
'
databaseName = Array("One", "Two", "Three")
Begin = Application.InputBox(Prompt:="Please Enter an Start Date.", Title:="Begin Date")
Finish = Application.InputBox(Prompt:="Please Enter an End Date.", Title:="End Date")
For q = LBound(databaseName) To UBound(databaseName)
Call ImportData(CStr(databaseName(q)), CDate(Begin), CDate(Finish))
Next q
End Sub
Public Sub ImportData(databaseName, Begin, Finish)
varConn = "ODBC;DBQ=C:\Test\" & databaseName & "db_rpt.mdb;Driver={Driver do Microsoft Access (*.mdb)}"
varSQL = Here is my SQL statement
With ActiveSheet.QueryTables.Add(Connection:=varConn, Destination:=Range("A1"))
.CommandText = varSQL
.Name = "Query-39008"
.RefreshStyle = xlInsertEntireRows
.FieldNames = True
.PreserveColumnInfo = True
.PreserveFormatting = True
.AdjustColumnWidth = True
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=True
End With
End Sub
[/vba]
The issue that I am having is that each time it cycles thro to the next databse it is placing the data to the right of the previous query. Ideally, what I want to happen is to have each subsequent query appended to the last row with data. However, if that isn't possible, another option (which please chime in and tell me other options also) I can think of is to (if this is possible) have each iteration pull in on a different workbook page, then copy the data from each worksheet onto a cumulative worksheet, and delete the individual worksheets before saving. But I feel that may be writing a whole lot of extra code.
How can I have the above code with each iteration append to the row below the last row with data?