DAO Access

** Cách viết ngắn gọn dùng biến ADO Access
With CurrentDb.OpenRecordset("Table1")
    .FindFirst "Cot=1"
    .AddNew '.Edit
    
    'Set col value
    '!Cot1 = "value 1"
        
       
    .Update
    .Close
End With


** Cách viết đẩy đủ ADO Access

Khai bao bien
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

' Bay loi
On Error GoTo ErrorHandler
 
 Set dbs = CurrentDb
 strSQL = "SELECT * FROM tbTest"
 Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
 
 ' Move First
 rst.MoveFirst
 MsgBox rst!ID

 ' Move Last
 rst.MoveLast
 MsgBox "TONG " & rst.RecordCount
 
 
  'Them moi
 With rst
    .AddNew
    ''Set Col
    !HoTen = "A"
    '''''''
    .Update
 End With
 
 ' update
 With rst
    .FindFirst "ID=1"
    .Edit
    ''Set Col
    !HoTen = "AAAAAAAAAAAAAAAA"
    ''''''''
    .Update
 End With
 
 ' Loop All row
 rst.MoveFirst
 Dim strLoopAdd As String
 Do While Not rst.EOF
      If Not IsNull(rst!ID) Then
        strLoopAdd = strLoopAdd + CStr(rst!ID)
      End If
      rst.MoveNext
 Loop
 MsgBox strLoopAdd
 ' END:Loop All row

  ' Sử dụng FOR i=0

rst.MoveLast
rst.MoveFirst

  For i = 1 To  rst.RecordCount  'Chú ý từ 1
        MsgBox rst!TenCot
        rst.MoveNext    
   Next

  '

 
 ' Clear var
 rst.Close
 dbs.Close
 Set rst = Nothing
 Set dbs = Nothing
 Exit Sub
 ''''''''''''''''''''''''''''''''
ErrorHandler:
   MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description