-
RE: Xuất giá trị từ các Text Box ra file TXT
quyvt1993 > 29-06-16, 03:47 PM
(04-01-16, 09:35 PM)ongke0711 Đã viết: Cách làm này của tôi chỉ xuất nhưng gì hiển thị trên textbox trên form, không xuất dữ liệu từ table hay query của form.
Share với bạn 2 kiểu xuất: Excel và Txt.
- Tạo thêm 1 textbox dùng để lưu cái chuỗi tổng hợp như bạn yêu cầu [txtTongHop].
- Viết 2 cái Sub, XuatExcel và writeText (dùng FileSystemObject). Code như bên dưới.
Mã PHP: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
File demo: http://www.mediafire.com/download/lo1xg9...T%29.accdb
ongke ơi, mình đã test dc code trên của bạn, nhưng mình sửa lại thay vì chép vào file txt có sẵn, mình cho nó xuất thành file txt mới rồi ghi lên file đó theo ý mình.
Mã:Dim name As String
name = Day(Date) & Month(Date) & Year(Date) & "_" & Hour(Time()) & Minute(Time()) & Second(Time())
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 = CurrentProject.Path & "\" & name & ".txt"
'FileName = CurrentProject.Path & "\log.txt"
Set MyFile = fso.CreateTextFile(FileName, True, -1)
'Set MyFile = fso.OpenTextFile(FileName, 2, True, -1) '-1: Unicode; 0: ASCII; -2: System default'
MyFile.WriteLine txtnhatky
MyFile.Close