Hello, I'm pretty new to VBA, and I've been able to set-up a macro so that I can run multiple queries (all of which come from different tables on the same server). My issue is that with each query I'm having to select the data source and login over and over again. This is despite the fact that it is the exact same thing. Is there a way to run all of these queries at the same time without having to select the data source each time?
I think it might have to do with the VBA code creating a new connection for every single query, but I am not knowledgeable enough to know for sure. The code I'm using for each one looks like the example below:
SQLString = Range("EXPrevCurrentString") ' Insert the name of the range/cell where the query is stored here
'Update the connection string with the server
ConnectionString = Replace(ConnectionString, "@ServerName", Server)
'Update the query string with the selected MarketID and FlowDate
SQLString = Replace(SQLString, "@MarketID", MarketID)
SQLString = Replace(SQLString, "@EXArchiveDate", EXArchiveDate)
'Query to get the data with the "Current" Tag
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DSN=__________;UID=_________;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=________;DATABASE=_______" _
, Destination:=Range("$A$1")).QueryTable
.CommandText = SQLString
.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_EXPrevCurrent"
.Refresh BackgroundQuery:=False
End With
Thanks and I appreciate any help you can give!
I think it might have to do with the VBA code creating a new connection for every single query, but I am not knowledgeable enough to know for sure. The code I'm using for each one looks like the example below:
SQLString = Range("EXPrevCurrentString") ' Insert the name of the range/cell where the query is stored here
'Update the connection string with the server
ConnectionString = Replace(ConnectionString, "@ServerName", Server)
'Update the query string with the selected MarketID and FlowDate
SQLString = Replace(SQLString, "@MarketID", MarketID)
SQLString = Replace(SQLString, "@EXArchiveDate", EXArchiveDate)
'Query to get the data with the "Current" Tag
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DSN=__________;UID=_________;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=________;DATABASE=_______" _
, Destination:=Range("$A$1")).QueryTable
.CommandText = SQLString
.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_EXPrevCurrent"
.Refresh BackgroundQuery:=False
End With
Thanks and I appreciate any help you can give!