2015-10-23 2 views
0

Ниже приведен код: То, что я пытаюсь сделать, - это обновление данных под моим контролем. Поэтому, когда я «GL date» с 03/31/2014 по 04/31/2014. Соединение собирает данные апреля.Ошибка связи OLEDB Excel

Ошибка, которую я получаю, с Selection.QueryTable, вот где она ломается. таблица начинается с ячейки «A1» на вкладке PCAP

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 
+0

Работает ли 'Selection.ListObject.QueryTable'? –

ответ

0

Возможно, диапазон «А1» не содержит объект таблицы запроса. Также старайтесь использовать 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», которая не объявлена ​​или не передана в качестве аргумента.

Надеюсь, это поможет :)