如何在 MS Access 中比较 2 个表的数据类型
How to compare data types of 2 tables in MS Access
有没有一种方法可以在 MS Access 中比较 2 table 的数据类型?我有一个 table 有 66 列,具有指定的名称(如 ID、名称、ETc...),另一个 table 将根据具有相同列数但字段名称的提取自动生成default (F1, F2, F3, ... F66) 所以想检查是否有一种访问方式可以比较来自两个 table 的 66 列的数据类型?非常感谢! :D
您可以使用一些 VBA 来循环每个 table 的 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", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function
我创建的函数 fDatatype
没有列出所有可用的可用数据类型 - 您可以通过按 F2 调出对象浏览器并搜索类似内容来查看完整列表dbText
查看 DAO.DataTypeEnum
class.
的所有成员
此致,
有没有一种方法可以在 MS Access 中比较 2 table 的数据类型?我有一个 table 有 66 列,具有指定的名称(如 ID、名称、ETc...),另一个 table 将根据具有相同列数但字段名称的提取自动生成default (F1, F2, F3, ... F66) 所以想检查是否有一种访问方式可以比较来自两个 table 的 66 列的数据类型?非常感谢! :D
您可以使用一些 VBA 来循环每个 table 的 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", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function
我创建的函数 fDatatype
没有列出所有可用的可用数据类型 - 您可以通过按 F2 调出对象浏览器并搜索类似内容来查看完整列表dbText
查看 DAO.DataTypeEnum
class.
此致,