• Xóa record trên form bằng hàm VBA
  • Xóa record trên form bằng hàm VBA

    Xuân Thanh > 25-04-12, 11:21 AM

    Nếu bạn không thích mã DoMenuItem do Wizard Button tạo ra để Xóa bản ghi hiện hành, bạn có thể sử dụng hàm sau để thay thế.

    Mã PHP:
    Function fDelCurrentRec(ByRef frmSomeForm As Form) As Boolean
        On Error 
    GoTo Err_Section
        
        With frmSomeForm
            
    If .NewRecord Then
                
    .Undo
                fDelCurrentRec 
    True
                
    GoTo Exit_Section
            End 
    If
        
    End With
        
        With frmSomeForm
    .RecordsetClone
            
    .Bookmark frmSomeForm.Bookmark
            
    .Delete
            frmSomeForm
    .Requery
        End With
        fDelCurrentRec 
    True
    Exit_Section
    :
        Exit Function

    Err_Section:
        
    fDelCurrentRec False
        Resume Exit_Section
    End 
    Function 

    Tại nút cmdXoa của form, bạn chèn câu lệnh sau :

    Mã PHP:
    Private Sub cmdXoa_Click()
        If 
    Not (fDelCurrentRec(Me)) Then
            MsgBox 
    "An Error occurred!"
        
    End If
    End Sub 

    (Sưu tầm)
    Thân mến