Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving the contents of a text box to a file 4

Status
Not open for further replies.

hankins1984

IS-IT--Management
Mar 11, 2002
198
GB
can some one please tell me how you the the contebts of a text box to a file.

i have tryed

text1.save ("c:\test.txt")

and nothing happens!!!

thanx Matthew Hankins

Network Technicain @ DGI Hereford
 
Could use the write or print method (see help for the difference)

Open "c:\test.txt" For Output As #1 ' Open file for output.
Write #1, "Hello World"
Write #1, ' Write blank line.
Close #1
 
You need to open a file for output and write the contents of the textbox to that file.

Code:
dim filenumber as long
dim filename as string

filenumber=freefile
filename = "c:\test.txt"
open filename for output as filenumber
print #filenumber, text1.text
close filenumber

You need not use freefile but I like to as opposed to hard coding a file number. If you use freefile you need to open the file before you use freefile again. Freefile gives the next available file number- so if you do something like this:

filenumber1 = freefile
filenumber2 = freefile

both variable will have the same value and if you try to open both you will get an error.

(just bonus info based on mistakes I have seen others make)

thanks,
Ron
 
Have you tried a search for 'save text file'? There are several answers already given. If you can't find one, try Justin Ezekiels simple answer in thread222-133731 Let me know if this helps

Check out FAQ222-2244
 
thanx 3 that Theologian, also is there a way of writing the data in the text box to a part of the .txt file.

eg: password = (data from text box)

thanx Matthew Hankins

Network Technicain @ DGI Hereford
 
Hi,
Once i send in the data to the text file, how do i read back the data? Also is there a way to just read some lines from the text file.
 
the api's WritePrivateProfileString and GetPrivateProfileString maybe what you are lloking for, have a look here:

it allows you to format a text file/ini file like this
[UserInfo]
name = blah

and then pull out the 'name' in 'UserInfo' section

Hope it helps

Grant
 
To read in the message is like writing almost. Just this time your opening the file for "Input" instead of "Output"


Dim pstrMyfileText as String
Open "C:\myfile.text" for Input as #1
Input #1, pstrMyfileText
Close #1

text1.text = pstrMyfileText

Hope this helps!

Also, in reguards to just reading certain lines, VB has many string manipulation functions. I can't remeber all of them off the top of my head, but perform a search on String Manipulation. If I remeber correctly, there a LString, MidString, RString, and a few others.......
 
To Theologian,
I tried to use your suggestion, but I keep getting this error:
run-time error '424'
object required

here is my code if you could look at it and tell me where I am making my mistake I would really appreciate it.

Private Sub cmd_exit_Click()
End
End Sub

Private Sub cmd_save_it_Click()
Dim filenumber As Long
Dim filename As String

filenumber = FreeFile
filename = "c:\test.txt"
Open filename For Output As filenumber
Print #filenumber, text1.Text
Close filenumber

End Sub

Private Sub Form_Load()

End Sub

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)

End Sub

Private Sub txt_input1_Change()

End Sub


should I have put your code (in blue) in the segment marked txt_input1_change (red)?

thanx "Psybertek"
___________________________________________
"Computer junkies never die, they just upload."
 
I figured out where I made my mistake.
But I have another question, how, using the calander control can I pass the date clicked to a var. called datename,
I wish to use this as the filename instead of

filename = "C:\test file.txt"

so that it looks like this

filename = datename


any help would be greatly appreciated.

______________________________________________
"Computer junkies never die, they just upload."
 
Try this
Code:
Filename = "C:\" & Format(datename,"dd-mmm-yyyy") & ".txt"

To use today's date chnge the code to:
Code:
Filename = "C:\" & Format(date,"dd-mmm-yyyy") & ".txt"
Let me know if this helps

Check out FAQ222-2244
 
johnwm's code will work fine, just remember that if anyone else saves a file for the same day - it will write over any previously existing file for the same day.

If you will be writing multiple files w/the same date- make sure that you add some other information to the file name to make it unique.

Vb will not automatically 'warn' you if a file already exists w/the same name. When you open a file for output it will create a new - empty file.

You can open a file for append. If you do that and a file exists then it will add whatever you print to the file- to the end of the file. (as append means) If no file of that name exists- opening to append will create the file. This is good for things like logs, etc.

Another useful method is input. This allows you to open and read a file.

thanks,
Ron
 
I wanted to say thanx for your help,
I actualy figured out this little piece of code to do something similar.

filepath = "C:\"
datename = MonthView1.Value
filenumber = FreeFile
filename = filepath & datename & ".txt"
Open filename For Output As filenumber
Print #filenumber, txt_input1.Text
Print #filenumber,
Close filenumber


now my problem is how to read one of the text files based on the date clicked on. (ie) if I click on july 30th and there is a corrasponding file for that date I need it to open the file and display it in the same text box.
Obviously I need to do some error handling if there is no file for that date. <<scratching head>> still working on that part too.

I have to say that in the short time I have belonged to this forum, I have had several of my questions answered far better than I had hoped.

Kudo's to all of you, that seem to able to find the time to help those of us that, all too often seem to need it.

Psybertek
___________________________
&quot;Computer junkies never die, They just upload.&quot;
 
to Theologian,

if I use the append to add more data to my text file that should take care of my file over write problem ,,, maybe ?
I hope.
thanx again

Psybertek[bigglasses]
____________________________
&quot;Computer junkies never die, They just upload.&quot;

 
Does anyone know how to make the saved text a binary vaule and not a string vaule. What i would like to do is write the data in the text box into a reg file in binary

thanx Matthew Hankins

Network Technicain @ DGI Hereford
 
psybertek, this should take care of your problem:
Dim filenumber As Long
Dim filename As String
filenumber = FreeFile
filename = &quot;D:\Test.txt&quot;
' Verify replacement of output file if it exists
If Dir(filename) <> &quot;&quot; Then
Msg = &quot;Click YES to overwrite or click NO to append!&quot;
If MsgBox(Msg, vbYesNo + vbInformation + vbDefaultButton2, &quot;Overwrite or Append?&quot;) = vbYes Then
Open filename For Output As filenumber
Else
Open filename For Append As filenumber
End If
End If
Print #filenumber, Text1.Text
Close filenumber

Swi
 
Or does anyone have any idear of ways that might be better for what i want to do?
Matthew Hankins

Network Technicain @ DGI Hereford
 
SWI when i enter your code i get an error, the error is showing a problem with the line

filenumber = FreeFile

can you please tell me why

Matthew Hankins

Network Technicain @ DGI Hereford
 
Try this:

Dim filenumber As Long
Dim filename As String
filenumber = FreeFile
filename = &quot;D:\Test.txt&quot;
' Verify replacement of output file if it exists
If Dir(filename) <> &quot;&quot; Then
Msg = &quot;Click YES to overwrite or click NO to append!&quot;
If MsgBox(Msg, vbYesNo + vbInformation + vbDefaultButton2, &quot;Overwrite or Append?&quot;) = vbYes Then
Open filename For Output As #filenumber
Else
Open filename For Append As #filenumber
End If
End If
Print #filenumber, Text1.Text
Close #filenumber Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top