|
-
Mar 9th, 2010, 03:52 AM
#1
Thread Starter
Lively Member
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
-
Mar 9th, 2010, 04:15 AM
#2
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
-
Mar 9th, 2010, 07:45 AM
#3
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
-
Mar 9th, 2010, 07:49 AM
#4
PowerPoster
Re: How to clear contents of text file
 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
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Mar 9th, 2010, 09:50 AM
#5
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"
Last edited by CDRIVE; Mar 9th, 2010 at 10:22 AM.
Reason: Append code
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|