How to clear contents of text file
Dear All,
I wrote the code for uploading the text file into the vb application. It is working well once I uploaded and cleared that contents in vb application .once againg I tried to open the file then it showed me open dialog box. I clicked on cancle button then it is showing the previous text file data. My requirement is When I Clicked on cancle button if does not show the previous text file contents. I am mentioning my code bellow.
Code:
Private Sub Command2_Click()
On Error GoTo cancelthis
CommonDialog1.ShowOpen
file$ = CommonDialog1.FileTitle
Text2.text = ""
Open file$ For Input As #1
While Not EOF(1)
Input #1, a$
Text2.text = Text2.text + a$ + Chr$(13) + Chr(10)
Wend
Close #1
cancelthis:
End Sub
How can I remove the old text file contents
Please let me know the solution as early as possible
Regrds,
Venky
Re: How to clear contents of text file
I remember there was a property in CommonDialog to trap the cancel button. I don't have VB6 installed here, so I'm just trying to recall from my memory.
Code:
Private Sub Command2_Click()
On Error GoTo cancelthis
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
file$ = CommonDialog1.FileTitle
Text2.text = ""
Open file$ For Input As #1
While Not EOF(1)
Input #1, a$
Text2.text = Text2.text + a$ + Chr$(13) + Chr(10)
Wend
Close #1
cancelthis:
End Sub
Re: How to clear contents of text file
One thing you missed that is kind of important :)
Code:
Private Sub Command2_Click()
On Error GoTo cancelthis
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
file$ = CommonDialog1.FileTitle
Text2.text = ""
Open file$ For Input As #1
While Not EOF(1)
Input #1, a$
Text2.text = Text2.text + a$ + Chr$(13) + Chr(10)
Wend
Close #1
Exit Sub
cancelthis:
End Sub
Re: How to clear contents of text file
Quote:
Originally Posted by
Hack
One thing you missed that is kind of important :)
Only important if there is any code after cancelthis: which in this case there isn't...if he then ADDED code, perhaps you'd be right, but the idea of cancelling in my mind would mean there wouldn't be any more code
Re: How to clear contents of text file
Try this..
Note: Don't use math operators to concatenate strings. I changed your '+' to '&'.
Code:
Option Explicit
Dim file$
Dim a$
Private Sub Command1_Click()
On Error GoTo cancelthis
a$ = file$
CommonDialog1.CancelError = True
CommonDialog1.ShowOpen
file$ = CommonDialog1.FileTitle
Text2.Text = ""
Open file$ For Input As #1
While Not EOF(1)
Input #1, a$
Text2.Text = Text2.Text & a$ & Chr$(13) & Chr(10)
Wend
Close #1
Exit Sub
cancelthis:
If StrPtr(a$) = 0 Then MsgBox "User Clicked Cancel"
End Sub
You could also write it like this..
Code:
If Err = 32755 Then MsgBox "User Clicked Cancel"