Results 1 to 5 of 5

Thread: How to clear contents of text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    India
    Posts
    94

    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

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: How to clear contents of text file

    Quote Originally Posted by Hack View Post
    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.

  5. #5
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    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
  •  



Click Here to Expand Forum to Full Width