Public Function IsExistField(ByVal sTableName As String, _
ByVal sFieldName As String) As Boolean
'使用示例: IsExistField("订单表","订单日期") '检测订单表中是否有订单日期字段
Dim fld As DAO.Field'注意这个地方要改成DAO.Field,否则会出现类型不匹配的错误.
Dim rs As DAO.Recordset
IsExistField = False
Set rs = CurrentDb.OpenRecordset(sTableName)
Debug.Print rs.Fields.Count
For Each fld In rs.Fields
If fld.Name = sFieldName Then
IsExistField = True
Exit For
End If
Next
rs.Close
Set rs = Nothing
Set fld = Nothing
ExitHere:
Set rs = Nothing
Set fld = Nothing
Exit Function
ErrorHandler:
MsgBox Err.Description, vbInformation, "提示"
Resume ExitHere
End Function
ByVal sFieldName As String) As Boolean
'使用示例: IsExistField("订单表","订单日期") '检测订单表中是否有订单日期字段
Dim fld As DAO.Field'注意这个地方要改成DAO.Field,否则会出现类型不匹配的错误.
Dim rs As DAO.Recordset
IsExistField = False
Set rs = CurrentDb.OpenRecordset(sTableName)
Debug.Print rs.Fields.Count
For Each fld In rs.Fields
If fld.Name = sFieldName Then
IsExistField = True
Exit For
End If
Next
rs.Close
Set rs = Nothing
Set fld = Nothing
ExitHere:
Set rs = Nothing
Set fld = Nothing
Exit Function
ErrorHandler:
MsgBox Err.Description, vbInformation, "提示"
Resume ExitHere
End Function