7 Replies - 12958 Views - Last Post: 15 March 2008 - 06:39 PM Rate Topic: -----

#1 davkav   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 11-March 08

Writing to text file whilst keeping original text

Posted 13 March 2008 - 05:16 AM

Private Sub Write2_Click()

Dim fso As FileSystemObject
Dim Noob As File
Dim Noobs As TextStream

   
Const forwriting = 2
Set fso = New FileSystemObject

'Creates a FileObject
Set Noob = fso.GetFile("C:\Documents and Settings\dkavanagh\Desktop\project docs\Project Documents\VB Project\test.txt")
   
'Opens the file as TextStream
Set Noobs = Noob.OpenAsTextStream(forwriting, True)
   
Noobs.WriteLine vbNewLine + vbNewLine + "Output for Timer 1 Status" + vbNewLine + " " + DMY + " " + GMT + vbNewLine + vbNewLine + Text1.Text + vbNewLine + vbNewLine + "__________________________________________________"

End Sub



Basically all i want to do is add text to text file i.e. generate a report file with timestamps

I can write to the file no problem but if i enter more text into the text1.text box it overwrites the text already in the file, how do i get around this. I bet its something simple but i just cant seem to fix it

Thanks guys

Is This A Good Question/Topic? 0
  • +

Replies To: Writing to text file whilst keeping original text

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Writing to text file whilst keeping original text

Posted 13 March 2008 - 05:25 AM

This is happening because when you're opening it you're opening it ForWriting. To append text at the end of the file, you need to open the file ForAppending, like so


Private Sub Write2_Click()

Dim fso As FileSystemObject
Dim Noob As File
Dim Noobs As TextStream

  
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fso = New FileSystemObject

'Creates a FileObject
Set Noob = fso.GetFile("C:\Documents and Settings\dkavanagh\Desktop\project docs\Project Documents\VB Project\test.txt")
  
'Opens the file as TextStream
Set Noobs = Noob.OpenAsTextStream(ForAppending, TriStateUseDefault)
  
Noobs.WriteLine vbNewLine + vbNewLine + "Output for Timer 1 Status" + vbNewLine + " " + DMY + " " + GMT + vbNewLine + vbNewLine + Text1.Text + vbNewLine + vbNewLine + "__________________________________________________"

End Sub




Also, I'm not sure why the True is there in the OpenAsTextStream signature
Was This Post Helpful? 0
  • +
  • -

#3 davkav   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 11-March 08

Re: Writing to text file whilst keeping original text

Posted 13 March 2008 - 05:33 AM

Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0


Whats the purpose of Tristate??

Set Noobs = Noob.OpenAsTextStream(ForAppending, TristateUseDefault)


Im getting an error at this point "Invalid procedure call or arugument", it doesnt seem to know what ForAppend is

Sorry to be a pain

This post has been edited by davkav: 13 March 2008 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Writing to text file whilst keeping original text

Posted 13 March 2008 - 05:37 AM

Change it

Set Noobs = Noob.OpenAsTextStream(ForAppending, True)



Sorry about that, I was thinking of VBScript
Was This Post Helpful? 0
  • +
  • -

#5 davkav   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 11-March 08

Re: Writing to text file whilst keeping original text

Posted 13 March 2008 - 05:45 AM

Got it ForAppending = 8 not 3

Thanks for the help PsychoCoder

One last question is there a way of adding the text into the file so that it shifts the present text down below it.

At the moment the text is being written under the text already in the file.

i.e.:

Present Text
Added Text

Where as I want

Added Text
Present Text

This post has been edited by davkav: 13 March 2008 - 05:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Writing to text file whilst keeping original text

Posted 13 March 2008 - 05:55 AM

Well looks like we're going to have to take a different approach, Ive yet to have this kind of problem with something like OpenAsTextSteam, so we'll try this approach:


Private Sub Command1_Click()
'Create a variable to hold what we want to write
Dim textToWrite As String

'Set the value of our variable
textToWrite = vbNewLine + vbNewLine + "Output for Timer 1 Status" + vbNewLine + " " + DMY + " " + GMT + vbNewLine + vbNewLine + Text1.Text + vbNewLine + vbNewLine + "__________________________________________________" 

'Open our file "ForAppend", lets us append to the end of the file
Open "C:\Documents and Settings\dkavanagh\Desktop\project docs\Project Documents\VB Project\test.txt" For Append As #2

'Write our text to the file
Print #2, textToWrite

'Close the file up
Close #2
End Sub


Was This Post Helpful? 0
  • +
  • -

#7 davkav   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 11-March 08

Re: Writing to text file whilst keeping original text

Posted 13 March 2008 - 07:06 AM

Got that code working but its still doing same thing i.e. writing the entry after the text already in the file

I take it that id have to basically count the number of lines of text within the text file and move the text down said number so i can enter the new text in??

would this be the right way?

This post has been edited by davkav: 13 March 2008 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 rikki_max   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 19-April 07

Re: Writing to text file whilst keeping original text

Posted 15 March 2008 - 06:39 PM

the simplest way i use is to read text from a file than delete it after that write text to that file and that has never let me down
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1