thucgia > 30-10-15, 01:19 PM
Function get_field_Auto(tbl As String) As String
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim kq As String
kq = ""
Set db = CurrentDb()
Set tdf = db.TableDefs(tbl)
For Each fld In tdf.Fields
If fld.Attributes And dbAutoIncrField Then
kq = fld.Name
End If
Next
get_field_Auto = kq
End Function
Sub find_auto(tbl As String)
Dim rs As ADODB.Recordset
Dim f As ADODB.Field
Set rs = New ADODB.Recordset
rs.ActiveConnection = CurrentProject.Connection
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open "select * from " & tbl
For Each f In rs.Fields
Debug.Print f.Name & " : " & _
" type : " & f.Type & _
" attr : " & CBool(f.Attributes And DB_AUTOINCRFIELD) & _
""
Next f
Set rs = Nothing
End Sub
'find_auto("phieu")
'id : type : 3 attr : True
'ma_phieu : type : 202 attr : False
'ngay : type : 7 attr : True
'to_chuc_id : type : 202 attr : False
'tien : type : 5 attr : True
'loai : type : 202 attr : False
Minh Tiên > 31-10-15, 01:27 PM
thucgia > 01-11-15, 03:37 AM
(31-10-15, 01:27 PM)Minh Tiên Đã viết: Bạn thử tham khảo 2 link này !
https://msdn.microsoft.com/en-us/library...s.85).aspx
https://support.microsoft.com/en-us/kb/193947
Mong giúp được bạn.
Thân./.
Function getAuto(tbl As String) As String
'neu co "ten" else ""
On Error GoTo ErrHandler
Dim rs As ADODB.Recordset
Dim f As ADODB.Field
Dim kq As String
kq = ""
Set rs = New ADODB.Recordset
OpenMyConnect
rs.ActiveConnection = cnn 'CurrentProject.Connection
rs.CursorLocation = adUseServer
rs.CursorType = adOpenForwardOnly
rs.Open "Select * from " & tbl
For Each f In rs.Fields
If f.Properties("ISAUTOINCREMENT") = True Then
kq = f.Name
End If
Next f
getAuto = kq
ExitSub:
Set rs = Nothing
CloseMyConnect
Exit Function
ErrHandler:
MsgBox Err.Number & " -getAuto()- " & Err.Description
Resume ExitSub
End Function