473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

save to textfile

hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub

it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten
Nov 21 '05 #1
9 1704
Maarten,

How do you open the file in you SaveToTextFile method?

Check out the File Class's AppendText method.
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub

it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten

Nov 21 '05 #2
Hi,

Need to see code for SaveTextToFile to figure out problem.

Ken
---------------
"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub

it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten

Nov 21 '05 #3
Public Function SaveTextToFile(ByVal strData As String, _

ByVal FullPath As String, _

Optional ByVal ErrInfo As String = "") As Boolean

Dim Contents As String

Dim bAns As Boolean = False

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)

objReader.Close()

bAns = True

Catch Ex As Exception

ErrInfo = Ex.Message

End Try

Return bAns

End Function

thanks Maarten

"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub

it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten

Nov 21 '05 #4
Hi,

You are creating a new file each time you call savetexttofile and
writing one line to it. Try something like this.

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

For inti = 1 To 16

objReader.Write("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni")

Next

objReader.Close()
Catch Ex As Exception

ErrInfo = Ex.Message

End Try
End Sub
Ken
-----------------

"Maarten" <gu******@hotmail.com> wrote in message
news:41***********************@news.skynet.be...
Public Function SaveTextToFile(ByVal strData As String, _

ByVal FullPath As String, _

Optional ByVal ErrInfo As String = "") As Boolean

Dim Contents As String

Dim bAns As Boolean = False

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)

objReader.Close()

bAns = True

Catch Ex As Exception

ErrInfo = Ex.Message

End Try

Return bAns

End Function

thanks Maarten

"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub

it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten


Nov 21 '05 #5

"Maarten" <gu******@hotmail.com> wrote
Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)

Should be:
objReader = New StreamWriter(FullPath, True)


LFS
Nov 21 '05 #6
Maarten,

Most copied from Ken, however i thought he is overlooking something.

\\\
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click
Dim myWriter As StreamWriter
Try
myWriter = New StreamWriter( "C:\Documents and Settings\Maarten\My
Documents\PLC.uni"))
For inti = 1 To 16
myWriter.Writeline("Channel" & " " & inti & " = " & dchanPos(inti))
Next
myWriter.Close()
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Sub
///

Cor
Nov 21 '05 #7
Maarten,

Change:

objReader = New StreamWriter(FullPath)

to:

objReader = New StreamWriter(FullPath,True)

to append, rather than replace. Currently your code replace the text in the
file on each call to SaveTextToFile - that's why you only see the last line
saved.

For an explanation see:
http://msdn.microsoft.com/library/de...ctorTopic4.asp

NOTE: Unless there is a special reason to have a separate method to open,
save, and close the file for each line to be written (SaveTextToFile), you
should do it all in one method for efficiency e.g. Open the file, write all
the lines, close the file instead of opening and closing the file over and
over again.
--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Maarten" <gu******@hotmail.com> wrote in message
news:41***********************@news.skynet.be...
Public Function SaveTextToFile(ByVal strData As String, _

ByVal FullPath As String, _

Optional ByVal ErrInfo As String = "") As Boolean

Dim Contents As String

Dim bAns As Boolean = False

Dim objReader As StreamWriter

Try

objReader = New StreamWriter(FullPath)

objReader.Write(strData)

objReader.Close()

bAns = True

Catch Ex As Exception

ErrInfo = Ex.Message

End Try

Return bAns

End Function

thanks Maarten

"Maarten" <gu******@hotmail.com> wrote in message
news:41**********************@news.skynet.be...
hi all when i do this:

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click

For inti = 1 To 16

Call SaveTextToFile("Channel" & " " & inti & " = " & dchanPos(inti),
"C:\Documents and Settings\Maarten\My Documents\PLC.uni", "Error")

Next

End Sub

it only saves the last loop so in the textfile stands Channel 16 = False

but i want al lines (16) are saved in the file

thanks Maarten


Nov 21 '05 #8
Hi,

Thanks it should have been new streamwritter

Ken
---------------
"Cor Ligthert" <no************@planet.nl> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...
Maarten,

Most copied from Ken, however i thought he is overlooking something.

\\\
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem3.Click
Dim myWriter As StreamWriter
Try
myWriter = New StreamWriter( "C:\Documents and Settings\Maarten\My
Documents\PLC.uni"))
For inti = 1 To 16
myWriter.Writeline("Channel" & " " & inti & " = " & dchanPos(inti))
Next
myWriter.Close()
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Sub
///

Cor

Nov 21 '05 #9
Ken,

Thanks it should have been new streamwritter


Thanks as well for that extra addition however with a lot of messages we
come together to a result.

:-)

Cor


Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
0
by: G.Esmeijer | last post by:
Friends, I have have filled a datagrid (flexgrid from C1Components) with the content of a textfile. Now I would like to save the content to an access table. I've tried the insert command I have...
2
by: Amjad | last post by:
How can I save the contents of a DataGrid (bound by a DataSet) in a text file Also, how can I "Select All" rows of a DataGrid programmatically Amjad
1
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.