设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12下一页
返回列表 发新帖
查看: 4922|回复: 10

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

[复制链接]
发表于 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
复制代码

点击这里给我发消息

发表于 2011-12-7 22:35:21 | 显示全部楼层
本帖最后由 鱼儿游游 于 2011-12-7 22:58 编辑

斑竹,我试了,发现有以下问题,能不能再改进一下。
要是当前ACCESS数据库,可以取回自动编号的字段。
要是读取另一个ACCESS数据库(用ADO不用链接表)里的数据表,则不能取回自动编号的字段。
发表于 2011-12-8 09:10:17 | 显示全部楼层
把CNN 重设为打开另一个数据库的连接就行

点击这里给我发消息

发表于 2011-12-8 12:10:58 | 显示全部楼层
andymark 发表于 2011-12-8 09:10
把CNN 重设为打开另一个数据库的连接就行

我试了,不知道为什么不行。
 楼主| 发表于 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是你的库名,保存在一样的目录里

点击这里给我发消息

发表于 2011-12-8 13:14:23 | 显示全部楼层
本帖最后由 鱼儿游游 于 2011-12-8 13:21 编辑
Henry D. Sy 发表于 2011-12-8 12:37
abc是你的库名,保存在一样的目录里


我用你的方法,写成下面的函数,但仍然不能取回自动编号字段,不知道错哪了,帮忙看看,多谢了。
  1. Public Function GetIdentityField(strDatabaseName As String, strPwd As String, strTableName As String) As String
  2. On Error GoTo Err_Handler
  3.     Dim strFieldName As String
  4.     Dim cn           As Object
  5.     Dim rst          As Object
  6.     Dim strConnect   As String
  7.     Dim strSQL       As String
  8.     Dim intCount     As Integer
  9.     strFieldName = ""
  10.     '参数(数据库名)处理
  11.     If strDatabaseName Like ".\*" Then strDatabaseName = CurrentProject.Path & Mid(strDatabaseName, 2)
  12.     If Not strDatabaseName Like "[A-z]:*" Then strDatabaseName = CurrentProject.Path & "" & strDatabaseName
  13.     '定义ACCESS链接串
  14.     strConnect = "Provider=Microsoft.Jet.OLEDB.4.0" & ";Data Source=" & strDatabaseName & ";Jet OLEDB:Database Password=" & strPwd
  15.     '用ADO通过OLEDB直接连接数据库
  16.     Set cn = Nothing
  17.     Set cn = CreateObject("ADODB.Connection")
  18.     With cn
  19.         .ConnectionString = strConnect
  20.         .CursorLocation = adUseClient
  21.         .ConnectionTimeout = 15
  22.         .CommandTimeout = 30
  23.         .Open
  24.     End With
  25.     '打开ADO记录集
  26.     strSQL = "SELECT * FROM [" & strTableName & "]"
  27.     Set rst = Nothing
  28.     Set rst = CreateObject("ADODB.Recordset")
  29.     With rst
  30.         .Source = strSQL
  31.         .ActiveConnection = cn
  32.         .CursorLocation = adUseClient  '3
  33.         .CursorType = adOpenKeyset
  34.         .LockType = adLockReadOnly
  35.         .Open
  36.     End With
  37.     '查找自动编号字段
  38.     For intCount = 0 To rst.Fields.Count - 1
  39.          If rst.Fields(intCount).Properties("isautoincrement") Then
  40.              strFieldName = rst.Fields(intCount).Name
  41.              Exit For
  42.          End If
  43.     Next
  44.     rst.Close
  45.     cn.Close
  46. Exit_Handler:
  47.     GetIdentityField = strFieldName
  48.     Set rst = Nothing
  49.     Set cn = Nothing
  50.     Exit Function
  51. Err_Handler:
  52.     strFieldName = ""
  53.     MsgBox Err.Description, vbExclamation, "查询表标识列的列名出错"
  54.     Resume Exit_Handler
  55. End Function
复制代码
 楼主| 发表于 2011-12-8 15:21:03 | 显示全部楼层
视乎漏掉最终的赋值
GetIdentityField=strFieldName

点击这里给我发消息

发表于 2011-12-8 16:12:20 | 显示全部楼层
Henry D. Sy 发表于 2011-12-8 15:21
视乎漏掉最终的赋值
GetIdentityField=strFieldName

没有呀
 楼主| 发表于 2011-12-8 17:05:38 | 显示全部楼层
GetIdentityField=strFieldName
rst.Close
cn.Close

点击这里给我发消息

发表于 2011-12-8 17:27:46 | 显示全部楼层
本帖最后由 鱼儿游游 于 2011-12-8 19:19 编辑
Henry D. Sy 发表于 2011-12-8 17:05
GetIdentityField=strFieldName
rst.Close
cn.Close


关闭记录集、关闭连接不影响strFieldName的值呀。

多谢,斑竹,这问题我解决了,是游标的问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-18 21:09 , Processed in 0.116924 second(s), 34 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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