k3nzyyb > 04-01-16, 06:48 PM
Trích dẫn:loaiA=310, loaiB=680, loaiC=90
ongke0711 > 04-01-16, 09:35 PM
Sub XuatExcel()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Workbooks.Add 'Open ("D:\TestFile.xlsx")'
.Sheets("Sheet1").Select
.Range("A1") = Me.txtTongHop.Value
.Visible = True
'.ActiveWorkbook.Close True'
End With
Set xlApp = Nothing
End Sub
Sub writeText(strText As String)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine
Set fso = CreateObject("Scripting.FileSystemObject")
'Duong dan file .txt se xuat ra'
FileName = "D:\TestFile.txt"
Set MyFile = fso.OpenTextFile(FileName, 2, True, -1) '-1: Unicode; 0: ASCII; -2: System default'
MyFile.WriteLine strText
MyFile.Close
End Sub
Sub TongHopChuoi()
Dim strText As String
strText = Me.Label2.Caption & Me.Text1 - Me.Text8 & ", "
strText = strText & Me.Label4.Caption & Me.Text2 - Me.Text8 & ", "
strText = strText & Me.Label6.Caption & Me.Text3 - Me.Text8
Me.txtTongHop = strText
End Sub
Private Sub cmdXuatExcel_Click()
Call TongHopChuoi
Call XuatExcel
End Sub
Private Sub cmdXuatTxt_Click()
Call TongHopChuoi
Call writeText(Me.txtTongHop.Value)
End Sub
k3nzyyb > 05-01-16, 11:35 AM
Sub TongHopChuoi()
For i=0 to 70
strText[i] = Me.Label[i].Caption & Me.Text[i] - Me.Text8 & ", "
....
Me.txtTongHop = strText
End Sub
ongke0711 > 05-01-16, 11:58 AM
k3nzyyb > 05-01-16, 02:55 PM
(05-01-16, 11:58 AM)ongke0711 Đã viết: Nếu nhiều vậy thì đưa dữ liệu vô table rồi xuất cho nhanh bạn.
ongke0711 > 05-01-16, 03:11 PM
k3nzyyb > 05-01-16, 05:11 PM
ongke0711 > 05-01-16, 10:59 PM
Sub TongHopChuoi()
Dim strText As String
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblTemp", dbOpenDynaset)
rst.MoveFirst
strText = ""
With rst
Do Until rst.EOF
strText = strText & "Loai " & !Loai & " = " & !SLGiuLai & ", "
.MoveNext
Loop
End With
strText = Left(strText, Len(strText) - 2)
Me.txtTongHop = strText
End Sub
k3nzyyb > 06-01-16, 12:15 PM
(05-01-16, 10:59 PM)ongke0711 Đã viết: Để tạo table tạm "tblTemp" bạn dùng 2 cái query:
- qryTaotblTemp: tạo table với các dòng [Loai] đã sum số lượng và thêm 1 cột [SLGiuLai] có trị = 0.
- qryUpdateSLGiuLai: cập nhật vào cột [SLGiuLai] của tblTemp số lượng lấy từ form Test.
Sau đó viết code cho Sub TongHopChuoi () như sau:
Mã PHP:Sub TongHopChuoi()
Dim strText As String
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblTemp", dbOpenDynaset)
rst.MoveFirst
strText = ""
With rst
Do Until rst.EOF
strText = strText & "Loai " & !Loai & " = " & !SLGiuLai & ", "
.MoveNext
Loop
End With
strText = Left(strText, Len(strText) - 2)
Me.txtTongHop = strText
End Sub
Khi chạy, bạn chạy lần lượt qryTaotblTemp -> qryUpdate -> TongHopChuoi () -> Xuat text.
File demo: http://www.mediafire.com/download/lo1xg9...T%29.accdb
strText = strText & "Loai " & !Loai & " = " & !SSL - !SLGiuLai & ", "