Private Sub MSComm1_OnComm() Dim
fso As New FileSystemObject Dim
objFil As File Dim
objts As TextStream Dim
i As Integer Dim
file_name As String On
Error GoTo ErrH Select Case
MSComm1.CommEvent ' Event messages.
Case comEvReceive Dim
Buffer As Variant
Buffer = MSComm1.Input
'Send to Printer เมื่อต้องการส่งข้อมูลออกทางคอมพอร์ต2
'MSComm2.Output = Buffer
ShowData txtRXTX, (StrConv((Buffer), vbUnicode))
EventMsg$ = " Receive " 'บันทึกข้อมูลจาก
Buffer สู่ Text File
file_name = Trim(txtfilename.Text) Set
objFil = fso.GetFile("c:\" & file_name & ".txt") Set
objts = objFil.OpenAsTextStream(ForAppending)
objts.Write Buffer
objts.WriteLine
objts.Close
อย่าลืมโหลด Scrip Runtime ด้วย!
ไม่เช่นนั้นจะสร้าง Text File ไม่ได้
Private Sub Label1_Change()
'สำหรับสร้าง Text File แบบอัตโนมัติเมื่อวันที่เปลี่ยน
Dim date_old, date_now As
String Dim filename AsString
Dim fso As
New FileSystemObject Dim objFil
As File Dim
objts As TextStream
date_old = Trim(Mid(Label2.Caption, 1, 6))
date_now = Trim(Mid(Label1.Caption, 1, 6)) If
date_old <> date_now Then
filename = date_now'ชื่อไฟล์จะเปลี่ยนไปตามวันที่ของเครื่อง
txtfilename.Text = filename
Label2.Caption = Label1.Caption
fso.CreateTextFile ("c:\" & filename & ".txt") Set
objFil = fso.GetFile("c:\" & filename & ".txt") Else
filename = date_now
txtfilename.Text = filename On Error GoTo
err_file Set objFil = fso.GetFile("c:\"
& filename & ".txt")'ค้นหาว่ามีไฟล์อยู่ในระบบหรือไม่
End If
err_file: If Err.Number = 53 Then'สร้างไฟล์ขึ้นมาใหม่เมื่อค้นหาไฟล์ไม่เจอ
fso.CreateTextFile ("c:\" & filename & ".txt") End
If
'--------------------------------------------------------------------------------------------- Download Code
ได้ที่นี่.zip