設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊

QQ登錄

只需一步,快速開始

12下一頁
返回列表 發(fā)新帖
查看: 6330|回復(fù): 10
打印 上一主題 下一主題

[模塊/函數(shù)] 尋找表中自動(dòng)編號(hào)字段名函數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2011-12-7 22:14:24 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
  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
復(fù)制代碼
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享分享 分享淘帖 訂閱訂閱

點(diǎn)擊這里給我發(fā)消息

2#
發(fā)表于 2011-12-7 22:35:21 | 只看該作者
本帖最后由 魚兒游游 于 2011-12-7 22:58 編輯

斑竹,我試了,發(fā)現(xiàn)有以下問題,能不能再改進(jìn)一下。
要是當(dāng)前ACCESS數(shù)據(jù)庫,可以取回自動(dòng)編號(hào)的字段。
要是讀取另一個(gè)ACCESS數(shù)據(jù)庫(用ADO不用鏈接表)里的數(shù)據(jù)表,則不能取回自動(dòng)編號(hào)的字段。
3#
發(fā)表于 2011-12-8 09:10:17 | 只看該作者
把CNN 重設(shè)為打開另一個(gè)數(shù)據(jù)庫的連接就行

點(diǎn)擊這里給我發(fā)消息

4#
發(fā)表于 2011-12-8 12:10:58 | 只看該作者
andymark 發(fā)表于 2011-12-8 09:10
把CNN 重設(shè)為打開另一個(gè)數(shù)據(jù)庫的連接就行

我試了,不知道為什么不行。
5#
 樓主| 發(fā)表于 2011-12-8 12:37:13 | 只看該作者
魚兒游游 發(fā)表于 2011-12-7 22:35
斑竹,我試了,發(fā)現(xiàn)有以下問題,能不能再改進(jìn)一下。
要是當(dāng)前ACCESS數(shù)據(jù)庫,可以取回自動(dòng)編號(hào)的字段。
要 ...
  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
復(fù)制代碼
abc是你的庫名,保存在一樣的目錄里

點(diǎn)擊這里給我發(fā)消息

6#
發(fā)表于 2011-12-8 13:14:23 | 只看該作者
本帖最后由 魚兒游游 于 2011-12-8 13:21 編輯
Henry D. Sy 發(fā)表于 2011-12-8 12:37
abc是你的庫名,保存在一樣的目錄里


我用你的方法,寫成下面的函數(shù),但仍然不能取回自動(dòng)編號(hào)字段,不知道錯(cuò)哪了,幫忙看看,多謝了。
  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.     '參數(shù)(數(shù)據(jù)庫名)處理
  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直接連接數(shù)據(jù)庫
  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.     '查找自動(dòng)編號(hào)字段
  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, "查詢表標(biāo)識(shí)列的列名出錯(cuò)"
  54.     Resume Exit_Handler
  55. End Function
復(fù)制代碼
7#
 樓主| 發(fā)表于 2011-12-8 15:21:03 | 只看該作者
視乎漏掉最終的賦值
GetIdentityField=strFieldName

點(diǎn)擊這里給我發(fā)消息

8#
發(fā)表于 2011-12-8 16:12:20 | 只看該作者
Henry D. Sy 發(fā)表于 2011-12-8 15:21
視乎漏掉最終的賦值
GetIdentityField=strFieldName

沒有呀
9#
 樓主| 發(fā)表于 2011-12-8 17:05:38 | 只看該作者
GetIdentityField=strFieldName
rst.Close
cn.Close

點(diǎn)擊這里給我發(fā)消息

10#
發(fā)表于 2011-12-8 17:27:46 | 只看該作者
本帖最后由 魚兒游游 于 2011-12-8 19:19 編輯
Henry D. Sy 發(fā)表于 2011-12-8 17:05
GetIdentityField=strFieldName
rst.Close
cn.Close


關(guān)閉記錄集、關(guān)閉連接不影響strFieldName的值呀。

多謝,斑竹,這問題我解決了,是游標(biāo)的問題。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則

QQ|站長郵箱|小黑屋|手機(jī)版|Office中國/Access中國 ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2025-7-13 05:05 , Processed in 0.108955 second(s), 33 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表