Link to home
Start Free TrialLog in
Avatar of mcneil
mcneil

asked on

Newbie Q - File saving.

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

Avatar of simonbennett
simonbennett

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
...also look at....

Savesetting/getsetting - preferences should really be saved in the registry.
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...
DOH!!!! I just noticed you posted a question with the same title twice, mcneil. Sorry!
Avatar of mcneil

ASKER

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
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.
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
Avatar of mcneil

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of xlunax
xlunax

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
xlunax, for one thing, your "simple" code will append an extra line feed...
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...