Click to See Complete Forum and Search --> : Send data to file
isabelle
February 10th, 2000, 04:08 PM
Hi!!!
I need to know how to send data to a specific file. Not a database, just a text file.
Someone can help me with that?!
Isabelle
Johnny101
February 10th, 2000, 05:06 PM
There are two ways of doing this:
1) Works with native VB code, but not as flexible:
Sub WriteToFile(sTextToWrite as string)
Dim i as Integer
Dim fn as string
fn = App.Path & "\testing.txt"
i = FreeFile
Open fn for Output as i
print #i, sTextToWrite
Close i
End Sub
Works, but not very interesting.
2)Set a reference to the Microsoft Scripting Runtime
Sub WriteToFile(sTextToWrite as string)
Dim FSO as Scripting.FileSystemObject
Dim objWriter as Scripting.TextStream
Dim fn as string
set FSO = new Scripting.FileSystemObject
fn = App.Path & "\testing2.txt"
'the filename, how to open the file, can we create this file?
set objWriter = FSO.OpenTextFile(fn,ForWriting,true)
objWriter.Write sTextToWrite 'writes only the text supplied
objWriter.WriteLine sTextToWrite 'writes the text and places a vbCrLf at the end
objWriter.Close
set objWriter = nothing
set FSO = nothing
End Sub
I personally like the second way, because it's easier to append to an existing file, or just create the new file.
Good luck,
john
John Pirkey
MCSD
www.ShallowWaterSystems.com
Cakkie
February 11th, 2000, 12:00 AM
You can write data on several ways to a file. First you need to open a file
open "pathToFile" for input/output/append/binary as #filenum
Explanation:
"PathToFile" is the path to the file, when a full path is not specified, it is the file in the same directory as the program. Note that when developing, this is the directory where the vb.exe is located.
input: opens the file for read operations
output: opens the file and clears it. Used for output operations.
append: opens the file and appends the output at the end of the file. This creates a new file if the specified file does not exist.
binary: opens a file for binary access
#filenum: an integer value of a file used to refer to. Assign this value using the FreeFile statement.
eg.
myFileNum = FreeFile
open "myFile.dat" for input as #myFileNum
So, now the file is open, we can write to it. We can do this using the write or print command.
strName = "Isabelle"
print #myFileNum, "Name:" & strName
' this sends one line of data to a file, placing it on a new line
Write #myFileNum, "Name",strName
Now we have written what we had to write, we must close the file using the close command.
Close #myFileNum
The file we just written to should look like this:
Name:Isabelle
"Name","Isabelle"
The first line was written using the Print command, the second using the write command.
Now we may just want to retrieve that data we have written. This we do using the Line Input or Input. We open the file for input. and read from the file.
myFileNum = FreeFile
Open "myFile.dat" for input as #myFileNum
Line input #myFileNum, strLine 'reads a line from the file and places it in strData
input #myFileNum, strField, strValue
Close #myFileNum
Note that using the Input command, we specify 2 strings to get the values. This is because the line we read is "Name","Isabelle". "Name" will be assinged to strField, "Isabelle" will be assigned to strValue.
Tom Cannaerts
slisse@planetinternet.be
The best way to escape a problem, is to solve it.
smalig
February 11th, 2000, 02:40 AM
This is the simple method to save a file from a textbox in one call.
Dim fnumber as Integer
Dim fname as string
fnumber = FREEFILE
fname = "demo.txt"
Open fname for Output as #fnumber
print #fnumber, (Text1)
Close #fnumber
Please rate the solutions so that people feel happy and encourage to reply and answer your questions.
smalig@hotmail.com
http://vbcode.webhostme.com/
The Matrix
February 15th, 2000, 02:34 PM
these idiots that replied to you said to much bs. You just want the simple stuff right? here it is, this is just saving the text in a textbox to a simple text file:
open "c:\whatever.txt" for ouput as #1
input #1, text1
close #1
Johnny101
February 15th, 2000, 06:08 PM
Oh! Heaven forbid we offer options and explanations.
John Pirkey
MCSD
www.ShallowWaterSystems.com
Chris Eastwood
February 16th, 2000, 02:56 AM
>these idiots that replied to you said to much bs. You just want the
>simple stuff right? here it is, this is just saving the text in a textbox
>to a simple text file:
Oh panasonic / starcraft / The Matrix / Whatever you call yourself today - you have such a way with words, and can be so pleasant to the rest of the forum members.
All of the posts that replied to the original are far better than yours and for one reason - yours doesn't work.
>open "c:\whatever.txt" for ouput as #1
>input #1, text1
>close #1
So you're trying to convince us that the 'Input' statement in VB can be used to *write* data to a file ?
Plus the fact that you are using #1 as a file handle, when any programmer whos ever read a VB manual / helpfile will tell you to get a filehandle from the 'FreeFile' function first.
May I suggest that you think first, try out your code (and other peoples) responses before jumping in and flaming the other members of this group ?
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.