2016-02-24

EXCEL/VBA - Data connection query modification and refresh


strSQL = _
        "select " & _
            "'N/A' as UnionSource " & _
            ", 'ALL|' + Product + '|' + cast(OriginationYear as varchar(4)) as BusinessProductKey " & _
            ", 'ALL' as Business " & _
            ", Product " & _
strSQL = strSQL & _
        "from " & _
            "DATABASE.dbo.VIEW_OR_TABLE_NAME with(nolock) " & _
        "where " & _
            "1=1 " & _
            "and Product is not null " & _
            "and OriginationYear >= " & prmOriginationYearSince & " " & _
        "group by " & _
            "Product " & _
            ", OriginationYear " & _
        "order by " & _
            "1,2 " & _
        ";"
    Debug.Print strSQL
    With ActiveWorkbook.Connections("DATA_CONNECTION_NAME").ODBCConnection
        .BackgroundQuery = False
        .CommandText = strSQL
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .SourceDataFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    ActiveWorkbook.Connections("DATA_CONNECTION_NAME ").Refresh
   
   
   

No comments:

Post a Comment