技術(shù) 點(diǎn)
- 技術(shù)
- 點(diǎn)
- V幣
- 點(diǎn)
- 積分
- 3068

|
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ò)哪了,幫忙看看,多謝了。- Public Function GetIdentityField(strDatabaseName As String, strPwd As String, strTableName As String) As String
- On Error GoTo Err_Handler
- Dim strFieldName As String
- Dim cn As Object
- Dim rst As Object
- Dim strConnect As String
- Dim strSQL As String
- Dim intCount As Integer
- strFieldName = ""
- '參數(shù)(數(shù)據(jù)庫名)處理
- If strDatabaseName Like ".\*" Then strDatabaseName = CurrentProject.Path & Mid(strDatabaseName, 2)
- If Not strDatabaseName Like "[A-z]:*" Then strDatabaseName = CurrentProject.Path & "" & strDatabaseName
- '定義ACCESS鏈接串
- strConnect = "Provider=Microsoft.Jet.OLEDB.4.0" & ";Data Source=" & strDatabaseName & ";Jet OLEDB:Database Password=" & strPwd
- '用ADO通過OLEDB直接連接數(shù)據(jù)庫
- Set cn = Nothing
- Set cn = CreateObject("ADODB.Connection")
- With cn
- .ConnectionString = strConnect
- .CursorLocation = adUseClient
- .ConnectionTimeout = 15
- .CommandTimeout = 30
- .Open
- End With
- '打開ADO記錄集
- strSQL = "SELECT * FROM [" & strTableName & "]"
- Set rst = Nothing
- Set rst = CreateObject("ADODB.Recordset")
- With rst
- .Source = strSQL
- .ActiveConnection = cn
- .CursorLocation = adUseClient '3
- .CursorType = adOpenKeyset
- .LockType = adLockReadOnly
- .Open
- End With
- '查找自動(dòng)編號(hào)字段
- For intCount = 0 To rst.Fields.Count - 1
- If rst.Fields(intCount).Properties("isautoincrement") Then
- strFieldName = rst.Fields(intCount).Name
- Exit For
- End If
- Next
- rst.Close
- cn.Close
- Exit_Handler:
- GetIdentityField = strFieldName
- Set rst = Nothing
- Set cn = Nothing
- Exit Function
- Err_Handler:
- strFieldName = ""
- MsgBox Err.Description, vbExclamation, "查詢表標(biāo)識(shí)列的列名出錯(cuò)"
- Resume Exit_Handler
- End Function
復(fù)制代碼 |
|