Solved

How to Write to a File

Posted on 2002-03-18
7
120 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
> 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
Comment Utility
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
Comment Utility
gvsbnarayana,
'Microsoft Scripting Runtime' is not found in components, it is found as a reference.
0
 
LVL 8

Author Comment

by:gvsbnarayana
Comment Utility
Thank you TimCottee
Here are your points!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now