2009-06-01 73 views
2

當通過ODBC(特別是AS/400)鏈接到外部數據源時,我經常在另一方遇到隱藏的字段名稱,而數據字典不可用。在極少數情況下,我能夠從其他數據庫獲取字段描述,我希望能夠一次導入它們,而不是每次將一個描述複製/粘貼到表格設計表單中。如何填充MS Access中的字段描述

我無法在系統表中找到它,所以我不知道這個元數據存儲在哪裏。有關它在哪裏的任何想法,以及它是否可以批量更新?

更新:我設法使用OpenSchema方法(請參閱下面的代碼)讀取模式,但是這返回一個只讀數據集,使我無法更新描述。

Function UpdateFieldDescriptions() 
Dim cn As New ADODB.Connection 
Dim rs As ADODB.Recordset 
Dim rs2 As Recordset 
Dim strSQL As String 
Dim strDesc As String 

Set cn = CurrentProject.Connection 
Set rs = cn.OpenSchema(adSchemaColumns) 

While Not rs.EOF 
    If Left(rs!table_name, 4) <> "MSys" Then 
     Debug.Print rs!table_name, rs!column_name, rs!Description 
     strSQL = "SELECT Description " & _ 
      "FROM tblColumnDescriptions a " & _ 
      "WHERE a.Name = """ & rs!table_name & """ AND " & _ 
      "a.Column = """ & rs!column_name & """;" 
     Set rs2 = CurrentDb.OpenRecordset(strSQL) 
     While Not rs2.EOF 
      strDesc = rs2.Fields(0) 
      rs!Description = strDesc ' <---This generates an error 
     Wend 
    End If 
    rs.MoveNext 
Wend 

rs.Update 
rs.Close 
Set rs = Nothing 
Set rs2 = Nothing 
Set cn = Nothing 

End Function 

回答

1

一些注意事項可能有幫助。以下顯示來自adSchemaColumns的字段描述。

Function ListFieldDescriptions() 
    ''List field descriptions 
    Dim cn As New ADODB.Connection, cn2 As New ADODB.Connection 
    Dim rs As ADODB.Recordset, rs2 As ADODB.Recordset 

     Set cn = CurrentProject.Connection 

     Set rs = cn.OpenSchema(adSchemaTables, _ 
      Array(Empty, Empty, Empty, "tablenamehere")) 

     While Not rs.EOF 
      Debug.Print rs!table_name; " desc= "; rs!Description 
      Set rs2 = cn.OpenSchema(adSchemaColumns, _ 
       Array(Empty, Empty, "" & rs!table_name & "")) 
      While Not rs2.EOF 
       Debug.Print "  " & rs2!Column_Name 
       Debug.Print "  " & rs2!Data_Type 
       Debug.Print "  " & rs2!Description 
       Debug.Print "  " & rs2!Is_Nullable 
       rs2.MoveNext 
      Wend 
     rs.MoveNext 
     Wend 
     rs.Close 
     Set cn = Nothing 

    End Function  

來源:http://wiki.lessthandot.com/index.php/ADO_Schemas

+0

這是行不通的 - 的的OpenSchema調用返回一個空的記錄。我立即得到rs.EOF。 也許這是由於我的錶鏈接到ODBC數據源。但表和字段描述屬性仍然存儲在Access數據庫的本地,所以他們必須在某個地方... – 2009-06-19 15:21:08

1

使用DAO,而不是ADO:

Sub SetFieldDesc(TblName As String, FldName As String, Description As String) 
Dim db As DAO.Database, td As DAO.TableDef, fld As DAO.Field 

    Set db = CurrentDb() 
    Set td = db.TableDefs(TblName) 
    Set fld = td.Fields(FldName) 

    On Error Resume Next 
    fld.Properties("Description") = Description 
    If Err.Number = 3270 Then 'Property not found.' 
     fld.Properties.Append fld.CreateProperty("Description", dbText, Description) 
    End If 
End Sub