I have the following code in VBA, the problem is that if I email it to someone, it stops working. So I wanted to hard code the Add QueryTable feature. My question is, where do I add the Querytable.Add part ?
Dim strFile As String
Dim strPath As String
Dim strFilePath As String
Dim strQry As String
Dim strCmd As String
Dim strConn As String
Dim footprint As String
footprint = Application.InputBox("Enter the footprint:", "Input Box Text", Type:=2)
strPath = ActiveWorkbook.Path & "\"
strFile = ActiveWorkbook.Name
strFilePath = strPath & strFile
strQry = "Query from Excel Files"
strCmd = " SELECT `Roles$`.Name, `Foot_duties$`.duty"
strCmd = strCmd & " FROM " & strFilePath & ".`Foot_duties$` `Foot_duties$`,"
strCmd = strCmd & " " & strFilePath & ".`Roles$` `Roles$`"
strCmd = strCmd & " WHERE `Roles$`.Role = `Foot_duties$`.role
AND ((`Foot_duties$`.footprint='" & footprint & "'))"
strCmd = strCmd & " ORDER BY `Roles$`.Name"
strConn = "ODBC;DSN=Excel Files;DBQ=" & strPath & strFile
strConn = strConn & ";DefaultDir=" & strPath
strConn = strConn & ";DriverId=790;MaxBufferSize=2048;PageTimeout=5;"
'
With ActiveWorkbook.Connections(strQry).ODBCConnection
.BackgroundQuery = True
.CommandText = strCmd
.CommandType = xlCmdSql
.Connection = strConn
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections(strQry)
.Name = strQry
.Description = ""
End With
ActiveWorkbook.Connections(strQry).Refresh
' ActiveWorkbook.RefreshAll