Xuân Thanh > 25-07-18, 10:42 AM
Public Function VeTruoc()
On Error GoTo Err_VeTruoc
Application.DoCmd.GoToRecord , , acPrevious
Exit_VeTruoc:
Exit Function
Err_VeTruoc:
MsgBox "Day la mau tin dau"
Resume Exit_VeTruoc
End Function
Private Sub cmdTruoc_Click()
Call VeTruoc
End Sub
Public Function Locktxt(frmName As Form)
Dim cnt As Control
For Each cnt In frmName.Controls
If TypeName(cnt) = "TextBox" Then
cnt.Locked = True
End If
Next
End Function
Public Function UnLocktxt(frmName As Form)
Dim cnt As Control
For Each cnt In frmName.Controls
If TypeName(cnt) = "TextBox" Then
cnt.Locked = False
End If
Next
End Function
Option Compare Database
Option Explicit
Dim n As Integer
Public Function VeDau()
Application.DoCmd.GoToRecord , , acFirst
End Function
Public Function VeTruoc()
On Error GoTo Err_VeTruoc
Application.DoCmd.GoToRecord , , acPrevious
Exit_VeTruoc:
Exit Function
Err_VeTruoc:
MsgBox "Day la mau tin dau"
Resume Exit_VeTruoc
End Function
Public Function VeSau()
On Error GoTo Err_VeSau
Application.DoCmd.GoToRecord , , acNext
Exit_VeSau:
Exit Function
Err_VeSau:
MsgBox "Day la mau tin cuoi"
Call VeDau
Resume Exit_VeSau
End Function
Public Function VeCuoi()
Application.DoCmd.GoToRecord , , acLast
End Function
Public Function Locktxt(frmName As Form)
Dim cnt As Control
For Each cnt In frmName.Controls
If TypeName(cnt) = "TextBox" Then
cnt.Locked = True
End If
Next
End Function
Public Function UnLocktxt(frmName As Form)
Dim cnt As Control
For Each cnt In frmName.Controls
If TypeName(cnt) = "TextBox" Then
cnt.Locked = False
End If
Next
End Function
Public Function Them(frmName As Form)
n = 1
Call UnLocktxt(frmName)
Call ThemEn(frmName)
End Function
Public Function Luu(frmName As Form, TabName As String, Ma As String)
Dim Ans
Ans = MsgBox("Ban muon luu mau tin nay?", vbYesNo, "Luu hay khong")
If Ans <> vbYes Then
frmName.Undo
Call LuuEn(frmName)
Else
If n = 1 Then
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(TabName, dbOpenTable)
rs.Index = "PrimaryKey"
rs.Seek "=", Ma
If Not rs.NoMatch Then
MsgBox "Trung Ma", , "Chu y"
Exit Function
Else
Call LuuEn(frmName)
End If
Else
Call LuuEn(frmName)
End If
End If
frmName.Requery
Call Locktxt(frmName)
End Function
Public Function Sua(frmName As Form)
n = 2
Call UnLocktxt(frmName)
frmName.cmdThem.Enabled = False
frmName.cmdXoa.Enabled = False
frmName.cmdDau.Enabled = False
frmName.cmdTruoc.Enabled = False
frmName.cmdSau.Enabled = False
frmName.cmdCuoi.Enabled = False
frmName.cmdLuu.SetFocus
frmName.cmdSua.Enabled = False
End Function
Public Function LuuEn(frmName As Form)
frmName.cmdSua.Enabled = True
frmName.cmdXoa.Enabled = True
frmName.cmdDau.Enabled = True
frmName.cmdTruoc.Enabled = True
frmName.cmdSau.Enabled = True
frmName.cmdCuoi.Enabled = True
frmName.cmdThem.Enabled = True
End Function
Public Function ThemEn(frmName As Form)
frmName.cmdSua.Enabled = False
frmName.cmdXoa.Enabled = False
frmName.cmdDau.Enabled = False
frmName.cmdTruoc.Enabled = False
frmName.cmdSau.Enabled = False
frmName.cmdCuoi.Enabled = False
frmName.cmdLuu.SetFocus
frmName.cmdThem.Enabled = False
End Function
Public Function LoadForm(frmName As Form, TabName As String)
frmName.RecordSource = TabName
Call Locktxt(frmName)
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(TabName, dbOpenTable)
If rs.RecordCount = 0 Then
frmName.cmdSua.Enabled = False
frmName.cmdXoa.Enabled = False
frmName.cmdDau.Enabled = False
frmName.cmdTruoc.Enabled = False
frmName.cmdSau.Enabled = False
frmName.cmdCuoi.Enabled = False
End If
End Function
Public Function Xoa(frmName As Form, TabName As String, Ma As String)
Dim Ans
Ans = MsgBox("That su muon xoa mau tin nay?", vbYesNo, "Xoa Hay Khong")
If Ans = vbYes Then
DoCmd.SetWarnings (False)
Application.RunCommand acCmdDeleteRecord
MsgBox "Da xoa xong"
DoCmd.SetWarnings (True)
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset(TabName, dbOpenTable)
If rs.RecordCount = 0 Then
frmName.cmdSua.Enabled = False
frmName.cmdXoa.Enabled = False
frmName.cmdDau.Enabled = False
frmName.cmdTruoc.Enabled = False
frmName.cmdSau.Enabled = False
frmName.cmdCuoi.Enabled = False
Else
frmName.Repaint
Call VeDau
End If
Else
frmName.Repaint
Call VeDau
End If
End Function
Public Function ThoatForm(frmName As Form)
Application.DoCmd.Close
End Function
Option Compare Database
Option Explicit
Private Sub cmdCuoi_Click()
Call VeCuoi
End Sub
Private Sub cmdDau_Click()
Call VeDau
End Sub
Private Sub cmdLuu_Click()
Call Luu(Me, "tblKhachHang", Me.txtMa)
End Sub
Private Sub cmdSau_Click()
Call VeSau
End Sub
Private Sub cmdSua_Click()
Call Sua(Me)
End Sub
Private Sub cmdThem_Click()
Call Them(Me)
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub cmdThoat_Click()
Call ThoatForm(Me)
End Sub
Private Sub cmdTruoc_Click()
Call VeTruoc
End Sub
Private Sub cmdXoa_Click()
Call Xoa(Me, "tblKhachHang", Me.txtMa)
End Sub
Private Sub Form_Load()
Call LoadForm(Me, "tblKhachHang")
Me.txtMa.ControlSource = "MaKhach"
Me.txtTen.ControlSource = "TenKhach"
End Sub