office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access判断表字段是否存在

2017-12-08 16:27:00
tmtony8
原创
8732

有时候,我们需要往不同的表中动态添加数据。第一步就是先判断表中是否存在对应的字段。如果存在即添加,否则不添加。

如存在“职务”表,字段“职务”



判断表中是否存在该字段。参数stblname为表名,sfldName为字段名

Function BlnField(sTblName As String, sFldName As String) As Boolean
'    sTblName 要查找的表名
'    sFldName 要查找的字段名
    Dim fld As Field
    Dim rs As DAO.Recordset
    BlnField = False
    Set rs = CurrentDb.OpenRecordset(sTblName)
    rs.Fields.Refresh
    For Each fld In rs.Fields
        If fld.Name = sFldName Then
            BlnField = True
            Exit For
        End If
    Next
    rs.Close
    Set rs = Nothing
    Set fld = Nothing
End Function


窗体按钮调用函数

Private Sub Command5_Click()
    
    If BlnField(Me.Text1, Me.Text3) Then
        MsgBox "表字段存在"
    Else
        MsgBox "表字段不存在"
    End If
End Sub


如果表字段存在,提示“存在”


相反提示“不存在”

    分享