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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on loading a file into a textbox 1

Status
Not open for further replies.

MaTtRiX

IS-IT--Management
Aug 8, 2000
9
0
0
US
Hi.
I am a newbie to VB, and I am trying to learn more and more about it. Im trying to learn how to write things into a textbox, save it to a file, and then be able to open it again. Any points, or sites that might be able to help me with this would be greatly appreciated.

Thanks in advance!

 
Try:
[tt]
Open "MyFile.Txt" For Binary As #1
If LOF(1) > 32767 Then
GetFile$ = String$(32767, 32)
MsgBox "File is too large to fit in text box." & _
vbCrLf & "Click Ok to load the first part."
Else
GetFile$ = String$(LOF(1), 32)
End If
Get #1, 1, GetFile$
Close #1
Text1.Text = GetFile$
[/tt]
To write from text box
[tt]
Open "NewFile.Txt" For Binary As #1
Put #1, 1, Text1.Text
Close #1
[/tt]
Make sure you have the Multi-line property on the text box set to &quot;True&quot;. [sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>[/sig]
 
Thanks Alt! it worked Great! [sig][/sig]
 
You 're welcome. You might also want to check the size of the file, if it already exists, before you write to it. If the file is larger than the text box you will end up writing the contents of the text box to the beginning of the file, leaving whatever was previously written there at the end of the file.
Try: [tt]

Tfile$ = &quot;NewFile.Txt&quot;
If Dir(Tfile$) <> &quot;&quot; Then
If MsgBox(&quot;File already exists!&quot;, vbOKCancel) = vbCancel Then Exit Sub
If FileLen(Tfile$) < Len(Text1.Text) Then
Kill Tfile$
End If
End If
Open Tfile$ For Binary As #1
Put #1, 1, Text1.Text
Close #1
[/tt]
I hope this sets you off on the right foot (sorry about the slang).

[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>[/sig]
 
That worked great too, but, is there a way that i can append to a file that already exists without overwriting the file? i tried the append statement, but that didnt work too well. any suggestions on what i might be able to try? [sig][/sig]
 
You could open the file for Append and then add each line of the text box to the file using Print # statements (the slow way) or you could use my method of choice. Binary file writes are much faster than the disk action you see when files are open for Append or Output. Try:
[tt]
Open &quot;NewFile.Txt&quot; For Binary As #1
Put #1, Lof(1) + 1, Text1.Text
Close #1
[/tt]
This appends the contents of Text1 to the end of &quot;NewFile.Txt&quot; (starting at Lof(1)+1, that is, the byte following the end of the file) without all the looping nonsense associated with the Print # or Write # statements.

You just have to watch it. If you want to add Text1 starting on a new line in the file you should check to make sure the original file ends with VbCrLf (carrage return and line-feed) or that Text1 starts with VbCrLf. Otherwise the last line will continue with the first line of Text1.


[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>Don't sit down. It's time to dig another one.[/sig]
 
Alt, i must say, you are the man, this helps me out so much, Thanks a Bunch!!!! [sig][/sig]
 
Welcome again. Don't be afraid to play. That's how we iron out the wrinkes.
[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>Don't sit down. It's time to dig another one.[/sig]
 
Can any one tell me hold to open an .exe file like (sol.exe) when I push on a command button in VB6.0
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top