如何比较 MS Access 中 2 个表的数据类型

问题描述

有没有办法可以比较 MS Access 中 2 个表的数据类型?我有一个包含 66 列的表,其中包含指定名称(如 ID、名称、ETc...)和另一个表,该表将根据具有相同列数但字段名称认(F1、F2、F3、 ... F66)所以想检查是否有一种访问方式可以比较两个表中 66 列的数据类型?非常感谢! :D

解决方法

您可以使用一些 VBA 来循环每个表的 Fields 集合。像这样的事情应该让你开始:

Sub sCompareTables(strTable1 As String,strTable2 As String)
    On Error GoTo E_Handle
    Dim db As DAO.Database
    Dim tdf1 As DAO.TableDef
    Dim tdf2 As DAO.TableDef
    Dim lngLoop1 As Long
    Set db = CurrentDb
    Set tdf1 = db.TableDefs(strTable1)
    Set tdf2 = db.TableDefs(strTable2)
    If tdf1.Fields.Count = tdf2.Fields.Count Then
        For lngLoop1 = 0 To tdf1.Fields.Count - 1
            If tdf1.Fields(lngLoop1).Type = tdf2.Fields(lngLoop1).Type Then
                Debug.Print "Match: " & tdf1.Fields(lngLoop1).name & vbTab & tdf2.Fields(lngLoop1).name & vbTab & fDatatype(tdf1.Fields(lngLoop1).Type)
            Else
                Debug.Print "No Match: " & tdf1.Fields(lngLoop1).name & vbTab & tdf2.Fields(lngLoop1).name & vbTab & fDatatype(tdf1.Fields(lngLoop1).Type) & "|" & fDatatype(tdf2.Fields(lngLoop1).Type)
            End If
        Next lngLoop1
    Else
        Debug.Print "Field counts do not match"
    End If
sExit:
    On Error Resume Next
    Set tdf1 = Nothing
    Set tdf2 = Nothing
    Set db = Nothing
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "sCompareTables",vbOKOnly + vbCritical,"Error: " & Err.Number
    Resume sExit
End Sub

Function fDatatype(lngDatatype As Long) As String
    On Error GoTo E_Handle
    Select Case lngDatatype
        Case 4
            fDatatype = "Long"
        Case 8
            fDatatype = "Date"
        Case 10
            fDatatype = "Text"
        Case 12
            fDatatype = "Memo"
        Case Else
            fDatatype = "Unknown"
    End Select
fExit:
    On Error Resume Next
    Exit Function
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "fDatatype","Error: " & Err.Number
    Resume fExit
End Function

我创建的函数 fDatatype 没有列出所有可用的数据类型 - 您可以通过按 F2 打开对象浏览器并搜索类似 {{ 1}} 查看 dbText 类的所有成员。

问候,