Excel OLEDB 连接问题

Excel OLEDB connection issue

下面是代码:我想做的是让数据的刷新更受我的控制。所以当我 "GL date" 从 03/31/2014 到 04/31/2014。连接获取四月数据。

我得到的错误是 Selection.QueryTable,这就是它中断的地方。 table 从 PCAP 选项卡

上的单元格 "A1" 开始
    Sub Update()

    Call ReplaceConnectionandRefresh1("PCAP", "zzFS - PCAP- SCRF3", "Apollo", "zzFS - PCAP- SCRF3")

 End Sub


 Sub ReplaceConnectionandRefresh1(spreadsheet As Variant, DriverName As String, RWFolder As String, CombinedNumber As String)

    Sheets(spreadsheet).Visible = True
    Sheets(spreadsheet).Select
    Sheets(spreadsheet).Range("A1").Select
    With Selection.QueryTable
        .Connection = "OLEDB;Provider=ftiRSOLEDB.RSOLEDBProvider;" _
                    & "Integrated Security=" & """" & """" _
                    & ";Location=" & dbName & ";User ID=" & """" & """" _
                    & ";Initial Catalog=" & dbName & ";Data Source=" & ServerName _
                    & ";Mode=Read;Persist Security Info=True;Extended Properties="
        .MaintainConnection = False
         MYCURRENTVALUE = .CommandText
    End With
    MYCURRENTVALUE = """" & dbName & """"
    MYCURRENTVALUE = MYCURRENTVALUE & "." & """" & RWFolder & """"
    MYCURRENTVALUE = MYCURRENTVALUE & "." & """" & DriverName & """"

    MYCURRENTVALUE = MYCURRENTVALUE & " "
    MYCURRENTVALUE = MYCURRENTVALUE & """" & "Legal Entity=" & CombinedNumber & """"
    MYCURRENTVALUE = MYCURRENTVALUE & " " & """"


    MYCURRENTVALUE = MYCURRENTVALUE & "GL Date=" & Format("03/31/2014", "mm/dd/yyyy") & """"


    MYCURRENTVALUE = MYCURRENTVALUE & " FLAGS[/SILENT] "

    With Selection.QueryTable
        .CommandText = MYCURRENTVALUE
        .Refresh BackgroundQuery:=False
    End With

End Sub

可能范围 "A1" 不包含查询 table 对象。也尽可能少地使用 Selection 和 Select(实际上你会在极少数情况下需要它)而是使用实际对象。

Option Explicit

Sub Update()

    Call ReplaceConnectionandRefresh1("PCAP", "zzFS - PCAP- SCRF3", "Apollo", "zzFS - PCAP- SCRF3")

 End Sub


Sub ReplaceConnectionandRefresh1(spreadsheet As String, _
                                 DriverName As String, _
                                 RWFolder As String, _
                                 CombinedNumber As String)

    Dim oQueryTable As QueryTable
    Dim strConnnection As String
    Dim strCommand As String

    ' Grab the query Table from the sheet. I am grabbing the first one
    ' adjust if there is more.
    Set oQueryTable = Sheets(spreadsheet).QueryTables(1)


    Sheets(spreadsheet).Visible = True
    Sheets(spreadsheet).Select
    Sheets(spreadsheet).Range("A1").Select

    ' Create connection string
    strConnnection = "OLEDB;Provider=ftiRSOLEDB.RSOLEDBProvider;" _
                     & "Integrated Security=" & """" & """" _
                     & ";Location=" & dbName & ";User ID=" & """" & """" _
                     & ";Initial Catalog=" & dbName & ";Data Source=" & ServerName _
                     & ";Mode=Read;Persist Security Info=True;Extended Properties="

    'Create connection command
    strCommand = """" & dbName & """"
    strCommand = strCommand & "." & """" & RWFolder & """"
    strCommand = strCommand & "." & """" & DriverName & """"
    strCommand = strCommand & " "
    strCommand = strCommand & """" & "Legal Entity=" & CombinedNumber & """"
    strCommand = strCommand & " " & """"
    strCommand = strCommand & "GL Date=" & Format("03/31/2014", "mm/dd/yyyy") & """"
    strCommand = strCommand & " FLAGS[/SILENT] "

    ' Actually update the connection.
    With oQueryTable
        .Connection = strConnnection
        .MaintainConnection = False
        .CommandText = strCommand
        .Refresh BackgroundQuery:=False
    End With

End Sub

另请注意,有一个变量 "dbName" 未声明或作为参数传递。

希望对您有所帮助:)