School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,439 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,481 people online right now. Registration is fast and FREE... Join Now!




Saving Data into a Text file

 
Reply to this topicStart new topic

> Saving Data into a Text file, from a form

firebolt
Group Icon



post 2 Apr, 2009 - 11:10 PM
Post #1


Welcome to this Tutorial on saving data on a form into a text file.
This tutorial is meant to demonstrate one of many different ways that text within a form maybe saved into a text file in a drive. The form will have a button and the text will be directly entered on the form.

Firstly, you will need a Button called cmdSave and the caption Save. Then we basically start coding.

We first place in Option Explicit. This means it forces explicit declaration of all variables in a file.
CODE

Option Explicit


Now everything revolves around the Save button. We first declare all the variables: list, datafile and item.
CODE

Private Sub cmdSave_Click()
Dim list(5) As String * 10
Dim datafile As String
Dim item As Integer


Continuing on, we then list the text that we want to list in the form. We use "Print" to place in text within the form.
CODE

Cls: Print
Print "Text File Maker"

'the list of items
list(1) = "A"
list(2) = "B"
list(3) = "C"
list(4) = "D"
list(5) = "E"


Still under the Save button, we now tell the program to list the items in order ie. 1 to 5
CODE

Print "List in alphabetical order"
For item = 1 To 5
Print list(item)
Next item


This now prints the text in the form into a Text file. You may change the drive name and the text file name. The second part prints the text in order in the file.
CODE

Print
datafile = "D:\File.txt"

'open the file to receive data
Open datafile For Output As 1

For item = 1 To 5
Print #1, list(item)
Next item
Close #1


So there we have it. A program which saves text on a form into a text file or similar.

Thank you.

This post has been edited by firebolt: 3 Apr, 2009 - 09:25 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

firebolt
Group Icon



post 6 Apr, 2009 - 02:45 AM
Post #2
Please correct me if anything in this code is wrong.
Thanks.

This post has been edited by firebolt: 6 Apr, 2009 - 02:46 AM
Go to the top of the page
+Quote Post

Vortexfuel
*



post 19 Apr, 2009 - 08:09 PM
Post #3
well done coding one thing missing "END SUB" it's good for beginner

Print
datafile = "D:\File.txt"

'open the file to receive data
Open datafile For Output As 1

For item = 1 To 5
Print #1, list(item)
Next item
Close #1
End Sub
Go to the top of the page
+Quote Post

firebolt
Group Icon



post 19 Apr, 2009 - 09:30 PM
Post #4
Thanks for pointing that out! However, I dont have the rights to change it. Maybe you tell a mod or such to change it.

This post has been edited by firebolt: 19 Apr, 2009 - 09:31 PM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 01:06AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month