设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1598|回复: 2
打印 上一主题 下一主题

[其它] 求助:如何判断一个ACCESS字段是否为自动编号(已解决)

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2011-12-7 20:38:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 鱼儿游游 于 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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
2#
发表于 2011-12-7 22:02:13 | 只看该作者
  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
复制代码

点击这里给我发消息

3#
 楼主| 发表于 2011-12-7 22:03:33 | 只看该作者
本帖最后由 鱼儿游游 于 2011-12-8 19:22 编辑

多谢Henry D. Sy斑竹!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-2 22:40 , Processed in 0.077038 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表