2016-12-19 57 views
0

我已經使用此代碼鏈接到表格中,但我試圖將其更改爲View並且沒有鏈接,即使滿足條件。我應該改變什麼來鏈接視圖?使用前綴鏈接所有視圖

Dim c As ADODB.Connection 
Dim r As ADODB.Recordset 
Dim f As ADODB.Field 
Dim conn As String 
Set c = New ADODB.Connection 
With c 
.Provider = "sqloledb.1" 
With .Properties 
    .Item("Data Source") = "Server" 
    .Item("Initial Catalog") = "database" 
    .Item("PassWord") = "user" 
    .Item("User ID") = "pass" 
End With 
.Open 
Set r = .OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE")) 
With r 
    While Not .EOF 
    'Table_Type = Table works 
     If (.Fields("TABLE_TYPE") = "View" And .Fields("TABLE_NAME") Like "ZZ_*") Then 
       'Do something 
     End If 
     .MoveNext 
    Wend 
End With 

回答

0

你指定,當你調用.OpenSchema的「TABLE」這裏條件:

Set r = .OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE")) 

此過濾結果爲表設置,所以在循環.Fields("TABLE_TYPE")將永遠是「TABLE」(注意它總是大寫)。更換過濾器 「查看」 ......

Set r = .OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "VIEW")) 

...然後刪除多餘的測試在循環:

If .Fields("TABLE_NAME") Like "ZZ_*" Then 
     'Do something 
End If 
相關問題