Reading and writing text

Posted On 2018-02-04 by ChristopherPiestland
Tags: VB6 Files and I/O VB6 String Handling 
Views: 74

These two snippets of code demonstrate how to read a file, as well as how to save a file.

For both of these, the code is shown as it would appear in the click event of a button. However, you can trigger this with anything, such as a timer or a condition changing state.

In the firs snippet, I am demonstrating how to save the content of a text box to a text file. You can something similar to save the contents of an array or string variable.

Private Sub Command1_Click()
    Filename = InputBox$("Enter A Filename To Save The text")
    Open Filename for Output As #1
    Print #1, Text1.text
    Close #1

End Sub

For the second code snippet, I am showing how to load the content of a text file in to a text box. Similar to the output method above, the target for the text file data could also be a string variable. With slightly different code, you could also load the file into an array.

Private Sub Command2_Click()
    Filename = InputBox$("Enter The Filename To Open")
    Open Filename for Input As #1
    Text1.text = Input(LOF(1), 1)
    Close #1

End Sub

About the Author

has posted a total of 1 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links