Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
    
I'm confused - which part of your existing code do you want to replace with QueryTable? Are you sure the person you're emailing the existing code to doesn't just need to add a reference to their instance of Excel? –  sion_corn Jun 25 at 18:26
    
This question appears to be off-topic because OP can use the macro recorder to do this. –  David Zemens Jun 25 at 19:18
    
How do you add a reference ? Im new to this. –  user3776338 Jun 26 at 14:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.