Office中国论坛/Access中国论坛

标题: 求助:如何判断一个ACCESS字段是否为自动编号(已解决) [打印本页]

作者: 鱼儿游游    时间: 2011-12-7 20:38
标题: 求助:如何判断一个ACCESS字段是否为自动编号(已解决)
本帖最后由 鱼儿游游 于 2011-12-8 19:22 编辑

用ADO读取记录集,如何判断一个ACCESS字段是否为自动编号?

判断SQL数据库的我已解决,写了一个通用函数,调用即可。
'
'判断某列是否是标识列:是标识列,则返回:True、否则返回:False
Public Function IsIdentityField(ByVal strFieldName As String, ByVal strTableName As String) As Boolean
On Error GoTo Err_Handler
    Dim blnResult As Boolean
    Dim varValue  As Variant
    Dim rst       As Object
    Dim strSQL    As String
    blnResult = False
    varValue = 0
    strSQL = "SELECT COLUMNPROPERTY(OBJECT_ID('" & strTableName & "'),'" & strFieldName & "','IsIdentity')"
    If GetRecordset(rst, strSQL) Then
       If rst.RecordCount > 0 Then
             varValue = Nz(rst.Fields(0).Value, "NotFould")
       End If
       rst.Close
    End If
    If varValue = "NotFould" Then
        MsgBox "指定的数据表名[" & strTableName & "] 或 字段名[" & strFieldName & "]不存在!", vbExclamation, "提示"
    Else
        blnResult = varValue <> 0
    End If
Exit_Handler:
    IsIdentityField = blnResult
    Set rst = Nothing
    Exit Function
Err_Handler:
    blnResult = False
    MsgBox Err.Description, vbExclamation, "判断列是否是标识列出错"
    Resume Exit_Handler
End Function

作者: Henry D. Sy    时间: 2011-12-7 22:02
  1. '---------------------------------------------------------------------------------------
  2. ' Module    : IsAutoNum
  3. ' DateTime  : 2011-12-07 22:00
  4. ' Author    : Henry D. Sy
  5. ' Purpose   : AutoNumIs("tbl1")
  6. '---------------------------------------------------------------------------------------
  7. Public Function AutoNumIs(ByVal tblName As String) As String
  8.     Dim rs As New ADODB.Recordset
  9.     Dim cnn As New ADODB.Connection
  10.     Dim intCount As Integer
  11.     Dim strName As String
  12.     On Error GoTo AutoNumIs_Error

  13.     Set cnn = CurrentProject.Connection
  14.     rs.Open tblName, cnn, adOpenKeyset, adLockReadOnly
  15.     For intCount = 0 To rs.Fields.Count - 1
  16.         If rs.Fields(intCount).Properties("isautoincrement") Then
  17.             strName = rs.Fields(intCount).Name
  18.             Exit For
  19.         End If
  20.     Next
  21.     If Len(strName) <> 0 Then
  22.         AutoNumIs = strName
  23.     Else
  24.         AutoNumIs = "Not Find"
  25.     End If
  26.     rs.Close
  27.     Set rs = Nothing

  28.     On Error GoTo 0
  29.     Exit Function

  30. AutoNumIs_Error:

  31.     MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
  32. End Function
复制代码

作者: 鱼儿游游    时间: 2011-12-7 22:03
本帖最后由 鱼儿游游 于 2011-12-8 19:22 编辑

多谢Henry D. Sy斑竹!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3