maidinhdan > 09-03-20, 10:48 PM
HamThemDAOBeginTran 
'---------------------------------------------------------------------------------------
' Ten thu tuc/Procedure : HamThemDAOBeginTran
' Tac gia/ Author    : maidinhdan@gmail.com
' Ngay tao/Date      : 09/03/2020
' Cu phap/ Purpose  : HamThemDAOBeginTran
' Mo ta:
'---------------------------------------------------------------------------------------
'
Function HamThemDAOBeginTran()
Dim WsCurrent As DAO.Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
On Error GoTo Loi
  Set WsCurrent = DBEngine.Workspaces(0)  'Tao mot phien lam viec voi Database hien hanh
  Set db = CurrentDb
  Set rs = db.OpenRecordset("tblTest") 'Mo table tblTest
  WsCurrent.BeginTrans '=> Bat dau giao dich
    'Bat dau vong lap tu dau den cuoi cung
    ' Duyet de them moi vao table tblTest 5000 mau tin cot <=> rs.Fields(1) ; Cot [A] la AutoNumber
    Dim i As Integer    'Tao bien dem
    For i = 1 To 5000
        rs.AddNew  'Ra lenh them
        rs.Fields(1) = "Test them dong thu: " & i    'Them vao cot tuc la cot thu 2 voi noi dung tu 1 den 5000 cu the lap lai
        rs.Update
    Next i
    ' Xac nhan luu khong? ==> xac nhan chap nhan giao dich hay khong?
    If MsgBox("Ban co muon Luu cac thay cap nhat tren khong?", vbYesNo) = vbYes Then
        WsCurrent.CommitTrans    'Dong y giao dich: Luu cac thay doi va ket thuc transaction hien hanh
    Else
        WsCurrent.Rollback  'Huy giao dich: Huy bo cac thay doi cua transaction va ket thuc no
    End If
ThoatLoi:
    ' dong rs, cnn va giai phong bo nho
    rs.Close
    db.Close
'  WsCurrent.Close    'lam viec voi Database hien hanh khong can dung cai nay.
    
    Set rs = Nothing
    Set WsCurrent = Nothing
    Exit Function
        
Loi:
        'Neu loi phat sinh trong qua trinh cap nhat se chay cac doan lenh duoi day
        WsCurrent.Rollback  ' Huy giao dich
        Resume ThoatLoi
End Function 
'---------------------------------------------------------------------------------------
' Ten thu tuc/Procedure : HamSuaDAOBeginTran
' Tac gia/ Author    : maidinhdan@gmail.com
' Ngay tao/Date      : 09/03/2020
' Cu phap/ Purpose  : HamSuaDAOBeginTran
' Mo ta:
'---------------------------------------------------------------------------------------
' Nho dang ky thu vien DAO nhe: Microsoft DAO 3.6 Object Library
Function HamSuaDAOBeginTran()
Dim WsCurrent As DAO.Workspace
Dim db As DAO.Database
Dim rs As DAO.Recordset
On Error GoTo Loi
  Set WsCurrent = DBEngine.Workspaces(0)  'Tao mot phien lam viec voi Database hien hanh
  Set db = CurrentDb
  Set rs = db.OpenRecordset("tblTest") 'Mo table tblTest
    If rs.EOF Then GoTo ThoatLoi      'Neu khong co du lieu thi thoat
    rs.MoveFirst  ' Di chuyen den mau tin dau tien
    
  WsCurrent.BeginTrans '=> Bat dau giao dich
  Do Until rs.EOF
        rs.Edit    'rs.AddNew
        rs.Fields(1) = "Sua lai thanh: DAO_BeginTran"
        rs.Update
    rs.MoveNext
  Loop
    ' Xac nhan luu khong? ==> xac nhan chap nhan giao dich hay khong?
    If MsgBox("Ban co muon Luu cac thay cap nhat tren khong?", vbYesNo) = vbYes Then
        WsCurrent.CommitTrans    'Dong y giao dich: Luu cac thay doi va ket thuc transaction hien hanh
    Else
        WsCurrent.Rollback  'Huy giao dich: Huy bo cac thay doi cua transaction va ket thuc no
    End If
ThoatLoi:
    ' dong rs, cnn va giai phong bo nho
    rs.Close
    db.Close
'  WsCurrent.Close    'lam viec voi Database hien hanh khong can dung cai nay.
    
    Set rs = Nothing
    Set WsCurrent = Nothing
    Exit Function
        
Loi:
        'Neu loi phat sinh trong qua trinh cap nhat se chay cac doan lenh duoi day
        WsCurrent.Rollback  ' Huy giao dich
        Resume ThoatLoi
End Function 
ongke0711 > 11-03-20, 05:23 PM