设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

[模块/函数] 寻找表中自动编号字段名函数

[复制链接]
跳转到指定楼层
1#
发表于 2011-12-7 22:14:24 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
  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
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2011-12-8 12:37:13 | 显示全部楼层
鱼儿游游 发表于 2011-12-7 22:35
斑竹,我试了,发现有以下问题,能不能再改进一下。
要是当前ACCESS数据库,可以取回自动编号的字段。
要 ...
  1. Public Function AutoNumIs(ByVal tblName As String) As String
  2.     Dim rs As New ADODB.Recordset
  3.     Dim cnn As New ADODB.Connection
  4.     Dim intCount As Integer
  5.     Dim strName As String
  6.     On Error GoTo AutoNumIs_Error

  7.     'Set cnn = CurrentProject.Connection
  8.     cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
  9.         CurrentProject.Path & "/ABC.mdb;Jet OLEDB:database password="
  10.     cnn.Open
  11.     rs.Open tblName, cnn, adOpenKeyset, adLockReadOnly
  12.     For intCount = 0 To rs.Fields.Count - 1
  13.         If rs.Fields(intCount).Properties("isautoincrement") Then
  14.             strName = rs.Fields(intCount).Name
  15.             Exit For
  16.         End If
  17.     Next
  18.     If Len(strName) <> 0 Then
  19.         AutoNumIs = strName
  20.     Else
  21.         AutoNumIs = "Not Find"
  22.     End If
  23.     rs.Close
  24.     Set rs = Nothing

  25.     On Error GoTo 0
  26.     Exit Function

  27. AutoNumIs_Error:

  28.     MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
  29. End Function
复制代码
abc是你的库名,保存在一样的目录里
3#
 楼主| 发表于 2011-12-8 15:21:03 | 显示全部楼层
视乎漏掉最终的赋值
GetIdentityField=strFieldName
4#
 楼主| 发表于 2011-12-8 17:05:38 | 显示全部楼层
GetIdentityField=strFieldName
rst.Close
cn.Close
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-11 00:53 , Processed in 0.082598 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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