Page 1 of 1

สอบ ถามเกี่ยวกับการ รับค่าจาก text box ลง text file ครับ

Posted: Tue Aug 19, 2014 9:40 am
by jaktree26
สอบ ถามเกี่ยวกับการ รับค่าจาก text box ลง text file ครับ เพิ่งหัดเขียน VBA ครั้งแรก เลยยังงงๆครับ

โจทย์คือ ผมต้องการ รับค่าจาก Text Box1 และ Text Box2 แล้ว เซพลง ไฟล์ notepad ครับผม ตอนนี้ผมทำให้เขียนลง notepad ได้แล้ว

แต่ที่ติดปัญหาคือ เวลาไปกรอกใน Text Box อีกครั้งค่าใน notepad จะถูกอัพเดททับอ่ะครับ ซึ่งผมอยากให้มันเขียนต่อใน บรรทัดต่อไป รบกวนขอตัวอย่างโค้ดด้วยครับ

ขอบคุณครับ

Code: Select all

Private Declare Function MessageBeep& Lib "user32" (ByVal wType As Long)

Public Enum BeepTypes
    MB_OK = &H0&
    MB_ICONASTERISK = &H40&
    MB_ICONEXCLAMATION = &H30&
    MB_ICONHAND = &H10&
End Enum


Private Sub CommandButton1_Click()

End Sub

Private Sub Label1_Click()
Label1.TextAlign = fmTextAlignCenter

End Sub


Private Sub Label2_Click()

End Sub

Private Sub Label3_Click()

End Sub

Private Sub TextBox1_Change()


End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)


If KeyCode = 13 Then
    'MsgBox ("Return Pressed")
    TextBox2.Activate
    
    If Me.TextBox1.text = "" Then
        'Label1.Caption = "Please Enter Key!!"
        TextBox1.Activate
        
    Else
    
    
        
    End If
    
    

End If


End Sub

Private Sub TextBox2_Change()

End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)


If KeyCode = 13 Then
    If TextBox1.text = TextBox2.text Then
        'beep
        TestTheBeep1
        txt
        Label1.Caption = ""
        Label3.Visible = False
        Label2.Visible = True
        
         
        TextBox1.text = ""
        TextBox2.text = ""
        TextBox1.Activate
        
    
        
        
    Else
        TestTheBeep2
        txt
        Me.TextBox2.text = ""
        Label1.Caption = ""
        Label3.Visible = True
        Label2.Visible = False
        TextBox2.Activate
        
    End If
    
    
    
Else
    
End If

End Sub
 
Public Function BeepType(lSound As BeepTypes) As Long
     ' Function to return
    BeepType = MessageBeep(lSound)
End Function

 
Sub TestTheBeep1()
     ' Play the beep
    BeepType MB_ICONASTERISK
End Sub

Sub TestTheBeep2()
     ' Play the beep
    BeepType MB_ICONHAND
End Sub

Sub txt()

Dim objFSO, strFolder

Dim txtpass As String
Dim txtfail As String
txtpass = "PASS"
txtfail = "FAIL"

strFolder = "D:\2014"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
   objFSO.CreateFolder (strFolder)
   
End If


Open "D:\2014\Test.txt" For Output As #1
    If TextBox1.text = TextBox2.text Then
        'TextBox2.text = "Pass"
        Write #1, Me.TextBox1.text, Me.TextBox2.text, txtpass
    Else
        'TextBox2.text = "FAIL"
        Write #1, Me.TextBox1.text, Me.TextBox2.text, txtfail

    End If
Close #1
End Sub


Re: สอบ ถามเกี่ยวกับการ รับค่าจาก text box ลง text file ครับ

Posted: Tue Aug 19, 2014 1:46 pm
by snasui
:D ลองแนบไฟล์ตัวอย่างมาดูกันเพื่อจะได้ช่วยทดสอบได้ครับ