Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Write to a File

Posted on 2002-03-18
7
Medium Priority
?
128 Views
Last Modified: 2011-09-20
Can anyone explain me how to write to a file? I want to ask the user to give a file name and I need to write some data to the File. How to do this and what are the controls and/or components to be added to the project?
Thanks and Regards,
Badri
0
Comment
Question by:gvsbnarayana
7 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 150 total points
ID: 6877347
You don't need to add any controls:

Dim intFileHandle As Integer
intFileHandle = FreeFile()
Open "MyOutputFile.txt" For Output As #intFileHandle
Print #intFileHandle,"Here is Some Text"
Close #intFileHandle

Is all that you need to do.

To read from the file open it for "INPUT" and use Line Input #intFileHandle,strLine

To read an entire line back from the file.

There are also other associated functions that you can read up on in the help files.
0
 
LVL 9

Expert Comment

by:kprestage
ID: 6877352
The easiest and fastest way to do this is using the Microsoft Scripting host.  You will need to add a reference to it in your project.  Once you have that, the rest is pretty much self explanatory.  Create a File System Object, and a TextStream

dim fso as new filesystemobject
dim ts as TextStream

use the createtextfile method to create your file based on the path you want

set ts = fso.CreateTextFile(filename)

to write to your file use the write, or writeline method of the textstream

ts.write "Text you want to write to the file"


0
 
LVL 8

Author Comment

by:gvsbnarayana
ID: 6877484
Dear  kprestage ,
    I could not find that component name in my system and the component might be displayed with some other name as the names of the components will be displayed in French as my Operating system is a French version. So Can you give me the name of the dll ?
Thanks and Regards,
Badri
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:bobbit31
ID: 6877549
> The easiest and fastest way to do this is using the Microsoft Scripting host

actually, fso has more overhead than the native vb file i/o methods... therefore it is actually slower.  In addition, you need the scrrun.dll (i think that's the one) included w/ your package.  So for simple file i/o (ie. simply reading/writing to a file) i would strongly suggest just using the native routines as shown by TimCottee above.
0
 
LVL 9

Expert Comment

by:kprestage
ID: 6877714
As bobbit31 said, the file name for the dll is scrrun.dll.  It was my understanding that the native VB file I/O commands were being depreciated in VB.net, meaning, don't get accustomed to using them because they may not always be around.
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6877860
gvsbnarayana,
'Microsoft Scripting Runtime' is not found in components, it is found as a reference.
0
 
LVL 8

Author Comment

by:gvsbnarayana
ID: 6957994
Thank you TimCottee
Here are your points!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question