Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Newbie Q - File saving.

Posted on 1999-07-12
Medium Priority
203 Views
Ok,  I'm very new to VB, and I have  a simple question:  How do you save the contects of a textbox (which is in turn assigned to a variable) to disk?  I understand how to assign variables, open the common dialog and all that, but I don't seem to understand how to simply write the contents of a text box to disk!  Strangely, neither of the "learning edition" books I got with VB 6 explains this, aside from how to open the common dialog for loading and saving.

In essence, what I'm trying to do is have a screen of textboxes that the user fills out, and can then save as a sort of "preferences" file, so that they can simply reload them next time without having to enter it all in again.

If anyone could explain this, or give me a reference to where to look for an answer, I'd appreciate it.

Thanks!

Dana

0
Question by:mcneil
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2
• +2

LVL 6

Expert Comment

ID: 1523835
try this code....

dim FNum as long
dim Cnter as byte

FNum = freefile
open "c:\test.txt" for output as #FNum

for Cnter = 1 to 20

print #Fnum, trim(str(Cnter)) & " lines written."

next Cnter

close #Fnum

This will create a text file. Simply change the print statement to look at your textboxes.

e.g.

print #Fnum, txtMyTextBoxName

HTH
0

LVL 6

Expert Comment

ID: 1523836
...also look at....

Savesetting/getsetting - preferences should really be saved in the registry.
0

LVL 10

Expert Comment

ID: 1523837
This is weird. It's nothing to do with anyone here... but I posted an answer to this question. Not only do I now see someone else's answer, but whatever I posted doesn't show at all? That wasn't the first time this happened either!

Weird...
0

LVL 10

Expert Comment

ID: 1523838
DOH!!!! I just noticed you posted a question with the same title twice, mcneil. Sorry!
0

Author Comment

ID: 1523839
Simon,

That all looks like it makes sense to me, but a couple of questions....

Where is the test.txt file being created and written to?  It looks like the file is just read from, and not created using a number of text boxes.

Cnter is a counter yes?  And counts down the lines to be assigned to a particular textbox?  (ie. textbox12 = line12 in the text file?)

SOrry to reject the answer, but if you could explain how this is working to me, I'd really appreciate it! (And give you a great grade! :-)

-mcneil
0

LVL 1

Expert Comment

ID: 1523840
Q - Where is the test.txt file being created and written to?
A - THe statement  "open "c:\test.txt" for output as #FNum" the file is being written to the root dir of C:\ with a file name of test.txt.

To provide my own comment I would make your textboxes a control array and write the function like this:

dim FreeFileNum as long
dim Cnter as byte
dim myPath as string

FreeFileNum = freefile
myPath = App.path and "Textbox.txt"

open myPath for output as #FNum

for Cnter = 1 to 20

print #Fnum, MyTextBox(Cnter)

next Cnter

close #Fnum

This will create a text file. Simply change the print statement to look at your textboxes.
0

LVL 1

Expert Comment

ID: 1523841
McNeil,

I forgot to mention that since Simon originally had the idea He should be awarded the points, So please reject my answer and allow Simon an opportunity to post again.

Good Luck,

Jaysin
0

Author Comment

ID: 1523842
Ok,  Simon you can give an answer.

Let me give an example of what I'm getting at here.... I need a file to be created that is a sort of "preferences" for what the user is doing.  For example: The user enters in a number of items in text fields pertaining to thier automobile.  Color, type, size of engine, milage, etc...  Now the user has a number of vehicles, so they need a number of different preferences files for each one.  This is why I'm interested in using the common dialog box for loading and saving, so the user can specify what they want to call the file, to recall it for later use.

Now say one of the "preferences" for your car type is the gas mileage.  One of the forms in the program is a mileage calculator, so the user "opens" the data for thier car type (whcih they've already entered... lets say it's porche.dat") which alreday contains the Miles Per Gallon for that auto and the user can now just type in the miles, and automatically figure out range.... or something to that effect... you see what I'm getting at.

I guess what I'm getting at is this method just makes one file... I need to be able to have the user specify the name of the file(s), and be able to recall it later, depending on thier needs.

I guess I should have spelled this out in the first place.  Sorry for the trouble.  I'll increase the points for your effort!

-mcneil
0

LVL 1

Expert Comment

ID: 1523843
McNeil,

Just in case Simon chooses not to answer the question in a reasonable amount of time I will be glad to help you out.

Jaysin
0

Accepted Solution

xlunax earned 400 total points
ID: 1523844
Why does everyone try to showoff...here.

Open "c:\test.txt" For Output As #1
Print #1, Text1.Text
Close #1

Simple.   Change c:\text1.text to whatever you want.  Or if you wanna configure it just do sumthing like

theFile$= <path to file> Open theFile$ For Output As #1
Print #1, Text1.Text
Close #1

Just do that for all the textboxes.  Any questions email me at xlunax@rocketmail.com

0

LVL 10

Expert Comment

ID: 1523845
xlunax, for one thing, your "simple" code will append an extra line feed...
0

LVL 10

Expert Comment

ID: 1523846
and... if file #1 is already open it will fail. It's not a question of "showing off", it's a question of being thorough and catering for all eventualities...
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
###### Suggested Courses
Course of the Month7 days, 4 hours left to enroll