内置函数/模块
Access判断表字段是否存在
2015-02-10 16:16:13
文件类型 : rar
文件版本 : Access2003
简要说明 : 下面的函数就是检测表中某字段是否存在的函数。

有的时候,我们需要动态添加表字段,那么就需要判断某表中字段是否存在了。如果不存在,即添加;想反,存在,即不添加等等情况,我们都需要先判断某表的字段是否存在

下面的函数就是判断表中某字段是否存在的函数。

 

用途:检测某表中是否存在某个字段

'输入参数: sTableName 必需的,表名称。

'                 sFieldName  必需的,检测的字段名称
'返 回  值: true或者false
'使用示例: IsExistField("订单表","名称")

 

 

Public Function IsExistField(ByVal sTableName As String, _

                             ByVal sFieldName As String) As Booleanler
    Dim fld As Field
    Dim rs As DAO.Recordset
    IsExistField = False
    Set rs = CurrentDb.OpenRecordset(sTableName)
    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