Solved

How to Write to a File

Posted on 2002-03-18
7
124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Response and request in VB6 application 11 1,074
Hide vba in gp 7 127
Modifying Conditional Format from VBA code 3 85
Problem to line 23 81
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

752 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