我正在訪問數據庫上執行數據清理操作。我在一個表中有多個重複記錄,我想將它們合併成一個記錄。在這樣做時,我需要更新所有對我將整合的記錄的引用。MS Access元數據
如果我知道包含記錄ID的列名是否有一種方法可以找到訪問中包含此列的所有表?
我正在訪問數據庫上執行數據清理操作。我在一個表中有多個重複記錄,我想將它們合併成一個記錄。在這樣做時,我需要更新所有對我將整合的記錄的引用。MS Access元數據
如果我知道包含記錄ID的列名是否有一種方法可以找到訪問中包含此列的所有表?
簡答:是的。並且有很多方法可以使那隻貓變白。兩個想法:
(1)通過VBA,使用:Application.CurrentDb.TableDefs(一)點域(J),請將.Name
(2)通過工具==>分析==>文檔管理,做一個報告,然後搜索它的輸出(用MS Word發佈它)。
很抱歉,但Access沒有建立像MS SQL Server或DB2 - 在* MSys的表真的不設立查詢表模式類似。但是,其他人有基於VBA的解決方案看起來很有用。
我非常想找一個在查詢中完成它的方法。 – Dunc 2011-02-16 14:39:45
您可以檢查TableDefs集合並確定哪些表包含具有給定名稱的字段。
Public Sub TablesWithField(ByVal pName As String)
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim strMsg As String
Dim strName As String
On Error GoTo ErrorHandler
Set db = CurrentDb
For Each tdf In db.TableDefs
strName = vbNullString
'ignore system and temporary tables '
If Not (tdf.name Like "MSys*" Or tdf.name Like "~*") Then
strName = tdf.Fields(pName).name
If Len(strName) > 0 Then
Debug.Print tdf.name & ": " & pName
End If
End If
Next tdf
ExitHere:
On Error GoTo 0
Set tdf = Nothing
Set db = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3265 'Item not found in this collection. '
Resume Next
Case Else
strMsg = "Error " & Err.Number & " (" & Err.description _
& ") in procedure TablesWithField"
MsgBox strMsg
GoTo ExitHere
End Select
End Sub
您可以使用模式,不完全是一個查詢,但類似:
Function ListTablesContainingField(SelectFieldName) As String
'Tables returned will include linked tables
'I have added a little error coding. I don't normally do that
'for examples, so don't read anything into it :)
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim strTempList As String
On Error GoTo Error_Trap
Set cn = CurrentProject.Connection
'Get names of all tables that have a column called <SelectFieldName>
Set rs = cn.OpenSchema(adSchemaColumns, _
Array(Empty, Empty, Empty, SelectFieldName))
'List the tables that have been selected
While Not rs.EOF
'Exclude MS system tables
If Left(rs!Table_Name, 4) <> "MSys" Then
strTempList = strTempList & "," & rs!Table_Name
End If
rs.MoveNext
Wend
ListTablesContainingField = Mid(strTempList, 2)
Exit_Here:
rs.Close
Set cn = Nothing
Exit Function
Error_Trap:
MsgBox Err.Description
Resume Exit_Here
End Function
這並非罕見有在該字段爲關鍵還是國外不同的名稱(ID,PERSONID)。你有關係地圖嗎?這可能有幫助。順便說一句,你在談論一個獨特的鑰匙,你不是嗎? – Fionnuala 2011-02-16 14:34:15
@Remou是的我正在談論一個獨特的關鍵。似乎沒有定義任何關係,但似乎有一個約定,即外鍵具有與父表中相同的名稱。正如我所說知道列名我只想要一個方法來搜索該列的所有表。 – Dunc 2011-02-16 14:38:48