Solved

How to Write to a File

Posted on 2002-03-18
7
123 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 50 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
clicking a shape in a frame array vb6 3 47
Advice in Xamarin 21 97
Zip Folders Using Chilkat Routines 1 59
VBA - If Bookmark = "XXBOOKMARKXX" then 15 41
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

821 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