|
本帖最后由 鱼儿游游 于 2011-12-8 13:21 编辑
Henry D. Sy 发表于 2011-12-8 12:37
abc是你的库名,保存在一样的目录里
我用你的方法,写成下面的函数,但仍然不能取回自动编号字段,不知道错哪了,帮忙看看,多谢了。- 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 = ""
- '参数(数据库名)处理
- 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直接连接数据库
- 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
- '查找自动编号字段
- 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, "查询表标识列的列名出错"
- Resume Exit_Handler
- End Function
复制代码 |
|