hatashibl > 04-05-13, 06:22 PM
Private Sub Command8_Click()
Dim Ngaydauthang As Date
Dim Ngaycuoithang As Date
Dim rs As Recordset
Dim rs1 As Recordset
Dim CauSQL As String
Dim CauSQL1 As String
Dim CauSQL2 As String
Dim tongHH1 As Integer
Ngaydauthang = DateSerial(Me.txtnam, Me.txtthang, 1)
Ngaycuoithang = DateAdd("m", 1, Ngaydauthang) - 1
CauSQL = "Select Mshv from Thongtinhv"
Set rs = CurrentDb.OpenRecordset(CauSQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
Do Until rs.EOF
tongHH1 = 0
CauSQL1 = "Select * from Giaodich Where MST1 ='" & rs & "' and NgaythangGD<=Ngaycuoithang and NgaythangGD>=Ngaydauthang"
Set rs1 = CurrentDb.OpenRecordset(CauSQL1)
If rs1.RecordCount > 0 Then
rs1.MoveFirst
Do Until rs1.EOF
tongHH1 = rs1!Soluong * rs1!HH1
rs1.MoveNext
Loop
End If
CauSQL2 = "INSERT INTO PhatHH VALUES (rs, txtthang, txtnam, TgHHT1)"
DoCmd.RunSQL CauSQL2
rs.MoveNext
Loop
Else: MsgBox "Khong co Hoi vien de thong ke", vbDefaultButton1, "Thong bao"
End If
rs.Close
rs1.Close
End Sub
hatashibl > 06-05-13, 09:05 PM
Xuân Thanh > 07-05-13, 09:47 AM
hatashibl > 07-05-13, 12:03 PM
bomnhauag > 07-05-13, 12:22 PM
hatashibl > 07-05-13, 07:59 PM
bomnhauag > 07-05-13, 09:58 PM
Private Sub Command8_Click()
Dim s As String
thucHien
MsgBox "Da thuc hien xong..."
End Sub
Private Sub thucHien()
Dim r As DAO.Recordset
Dim s As String
'tao truy van tinh tong tien cua tung khach trong thang va nam duoc chon
s = "SELECT Giaodich.MST1, Sum(([Soluong]*[Gia])) AS SoTien "
s = s & "FROM Giaodich INNER JOIN HHGD ON Giaodich.STTGD = HHGD.STTGD "
s = s & "WHERE (((Month([NgaythangGD])) = Cint(" & txtthang & ")) And ((Year([NgaythangGD])) = Cint(" & txtnam & "))) "
s = s & "GROUP BY Giaodich.MST1;"
Set r = CurrentDb.OpenRecordset(s)
If r.EOF And r.BOF Then 'truong hop trong thoi gian do ko co phat sinh giao dich
MsgBox "Thang nay ko co phat sinh tien bac j het ah"
Else 'co giao dich thi duyet tung giao dich
r.MoveFirst
While Not r.EOF
'kiem xem trong PhatHH co thang Khach hang nay trong thoi gian do chua
s = Nz(DLookup("Mshv", "PhatHH", "Mshv='" & r("MST1") & "' And Thang=" & txtthang & " And Nam=" & txtnam), "")
'neu co mat no trong do thi sua lai noi dung can sua
If s <> "" Then
s = "UPDATE PhatHH SET PhatHH.HHtang1 = " & r("SoTien") & " WHERE (((PhatHH.Mshv)='" & r("MST1") & "') AND ((PhatHH.Thang)=" & txtthang & ") AND ((PhatHH.Nam)=" & txtnam & "));"
CurrentDb.Execute s
Else ' neu no klhong co trong PhatHH thi them cai mat moc cua no vo
s = "INSERT INTO PhatHH ( Mshv, HHtang1, Thang, Nam )SELECT '" & r("MST1") & "' , " & r("SoTien") & ", " & txtthang & ", " & txtnam & ";"
CurrentDb.Execute s
End If
r.MoveNext
Wend
End If
r.Close
Set r = Nothing
End Sub
hatashibl > 08-05-13, 11:19 AM
bomnhauag > 08-05-13, 03:29 PM
(08-05-13, 11:19 AM)hatashibl Đã viết: @: bomnhauag.
Cảm ơn bạn rất nhiều. Đúng là mình muốn các Khách hàng k phát sinh giao dịch trong tháng đó thì Lưu cả Mshv, txtthang, txtnam và tổng tiền = 0 vào table PhatHH.
Nhờ bạn sửa giúp mình luôn phần đó nhé.
Private Sub Command8_Click()
Dim s As String
thucHien
MsgBox "Da thuc hien xong..."
End Sub
Private Sub thucHien()
Dim r As DAO.Recordset
Dim s As String
'tao truy van tinh tong tien cua tung khach trong thang va nam duoc chon
's = "SELECT Giaodich.MST1, Sum(([Soluong]*[Gia])) AS SoTien "
's = s & "FROM Giaodich INNER JOIN HHGD ON Giaodich.STTGD = HHGD.STTGD "
's = s & "WHERE (((Month([NgaythangGD])) = Cint(" & txtthang & ")) And ((Year([NgaythangGD])) = Cint(" & txtnam & "))) "
's = s & "GROUP BY Giaodich.MST1;"
s = "SELECT Thongtinhv.Mshv AS MST1, QThem1.SoTien "
s = s & "FROM Thongtinhv LEFT JOIN QThem1 ON Thongtinhv.Mshv = QThem1.MST1 "
s = s & "WHERE (((QThem1.Thang)=Cint(" & txtthang & ")) AND ((QThem1.Nam)=Cint(" & txtnam & "))) OR (((QThem1.SoTien) Is Null));"
Set r = CurrentDb.OpenRecordset(s)
If r.EOF And r.BOF Then 'truong hop trong thoi gian do ko co phat sinh giao dich
MsgBox "Thang nay ko co phat sinh tien bac j het ah"
Else 'co giao dich thi duyet tung giao dich
r.MoveFirst
While Not r.EOF
'kiem xem trong PhatHH co thang Khach hang nay trong thoi gian do chua
s = Nz(DLookup("Mshv", "PhatHH", "Mshv='" & r("MST1") & "' And Thang=" & txtthang & " And Nam=" & txtnam), "")
'neu co mat no trong do thi sua lai noi dung can sua
If s <> "" Then
s = "UPDATE PhatHH SET PhatHH.HHtang1 = " & Nz(r("SoTien"), 0) & " WHERE (((PhatHH.Mshv)='" & r("MST1") & "') AND ((PhatHH.Thang)=" & txtthang & ") AND ((PhatHH.Nam)=" & txtnam & "));"
CurrentDb.Execute s
Else ' neu no klhong co trong PhatHH thi them cai mat moc cua no vo
s = "INSERT INTO PhatHH ( Mshv, HHtang1, Thang, Nam )SELECT '" & r("MST1") & "' , " & Nz(r("SoTien"), 0) & ", " & txtthang & ", " & txtnam & ";"
CurrentDb.Execute s
End If
r.MoveNext
Wend
End If
r.Close
Set r = Nothing
End Sub
hatashibl > 08-05-13, 07:45 PM