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