Excel OLEDB连接问题

时间:2015-10-23 14:19:33

标签: vba oledbconnection

下面是代码:我想要做的是让数据刷新更多在我的控制之下。因此,当我从2014年3月31日到2014年4月31日“GL日期”时。连接会获取4月的数据。

我得到的错误是使用Selection.QueryTable,这就是它破坏的地方。 该表从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

1 个答案:

答案 0 :(得分:0)

可能范围“A1”不包含Query表对象。也尝试尽可能少地使用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”未声明或作为参数传递。

我希望这会有所帮助:)

相关问题