pherotao > 03-01-18, 01:49 AM
(03-01-18, 12:12 AM)ongke0711 Đã viết:
Option Compare Database
Option Explicit
Public strEditAddStatus As String
Private Sub Form_Open(Cancel As Integer)
txtMaHH.Locked = True
txtTenHH.Locked = True
txtQuiCach.Locked = True
txtDVT.Locked = True
Xoa.Visible = True
Sua.Visible = True
Them.Visible = True
Ghi.Visible = False
Khong.Visible = False
End Sub
Private Sub List12_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[MaHH] = '" & Me![List12] & "'"
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
Private Sub Ghi_Click()
Dim DB As DAO.Database
Dim rs As DAO.Recordset
Set DB = CurrentDb
Set rs = DB.OpenRecordset("SELECT * FROM T_HangHoa")
Select Case strEditAddStatus
Case "Sua"
rs.FindFirst "[MaHH] = '" & Me.txtMaHH.Value & "'"
If Not rs.NoMatch Then
rs.Edit
rs("TenHH") = Me.txtTenHH.Value
rs("QuiCach") = Me.txtQuiCach.Value
rs("DVT") = Me.txtDVT.Value
End If
Case "Them"
rs.AddNew
rs("MaHH") = Me.txtMaHH.Value
rs("TenHH") = Me.txtTenHH.Value
rs("QuiCach") = Me.txtQuiCach.Value
rs("DVT") = Me.txtDVT.Value
End Select
rs.Update
Me.txtMaHH = Null
Me.txtTenHH = Null
Me.txtQuiCach = Null
Me.txtDVT = Null
Me.txtMaHH.Locked = True
Me.txtTenHH.Locked = True
Me.txtQuiCach.Locked = True
Me.txtDVT.Locked = True
Me.txtMaHH.SetFocus
rs.Close
Me.List.Requery
Ghi.Visible = False
Khong.Visible = False
Xoa.Visible = True
Sua.Visible = True
Them.Visible = True
End Sub
Private Sub Khong_Click()
Me.txtMaHH = Null
Me.txtTenHH = Null
Me.txtQuiCach = Null
Me.txtDVT = Null
Me.txtMaHH.Locked = True
Me.txtTenHH.Locked = True
Me.txtQuiCach.Locked = True
Me.txtDVT.Locked = True
Me.txtMaHH.SetFocus
Me.List.Requery
Ghi.Visible = False
Khong.Visible = False
Xoa.Visible = True
Sua.Visible = True
Them.Visible = True
End Sub
Private Sub List_Click()
On Error GoTo err_List_Click
List.Requery
txtMaHH = Me.List.Column(0)
txtTenHH = Me.List.Column(1)
txtQuiCach = Me.List.Column(2)
txtDVT = Me.List.Column(3)
exit_List_Click:
Exit Sub
err_List_Click:
MsgBox Err.Description
Resume exit_List_Click
End Sub
Private Sub Sua_Click()
Ghi.Visible = True
Khong.Visible = True
txtMaHH.Locked = False
txtTenHH.Locked = False
txtQuiCach.Locked = False
txtDVT.Locked = False
txtMaHH.SetFocus
Xoa.Visible = False
Sua.Visible = False
Them.Visible = False
Me.List.Requery
End Sub
Private Sub Them_Click()
Me.txtMaHH = Null
Me.txtTenHH = Null
Me.txtQuiCach = Null
Me.txtDVT = Null
txtMaHH.Locked = False
txtTenHH.Locked = False
txtQuiCach.Locked = False
txtDVT.Locked = False
Ghi.Visible = True
Khong.Visible = True
txtMaHH.SetFocus
Xoa.Visible = False
Sua.Visible = False
Them.Visible = False
End Sub
ongke0711 > 03-01-18, 07:46 AM
(02-01-18, 04:21 PM)ongke0711 Đã viết: - Tạo 1 cái biến tạm. Ví dụ: Public strEdiAddStatus As String
- Khi bấm nút [Sửa] thì gán biến strEditAddStatus = "Sua". Khi bấm nút [Them] thì gán biến strEditAddStatus = "Them".
pherotao > 03-01-18, 10:16 AM
(03-01-18, 07:46 AM)ongke0711 Đã viết: Thiệt potay với bạn.
Tôi có giải thích rõ ràng ở trên là:
(02-01-18, 04:21 PM)ongke0711 Đã viết: - Tạo 1 cái biến tạm. Ví dụ: Public strEdiAddStatus As String
- Khi bấm nút [Sửa] thì gán biến strEditAddStatus = "Sua". Khi bấm nút [Them] thì gán biến strEditAddStatus = "Them".
-> Vậy ở sự kiện bấm nút [Sửa] hay nút [Thêm] bạn có gán trị cho biến tạm strEditAddStatus chưa? -> Không có thì lấy gì mà "Select Case".
Private Sub Sua_Click()
strEditAddStatus = "Sua"
....
End Sub
-------------------
Private Sub Them_Click()
strEditAddStatus = "Them"
....
End Sub