Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to create text file with VB?

Posted on 2003-03-18
5
Medium Priority
?
434 Views
Last Modified: 2013-12-26
what are command in vb that use for create text file?
0
Comment
Question by:VB_SoftMan
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
supunr earned 150 total points
ID: 8164309

Dim FileNum as Long
Dim FileName as String
 
FileName = "C:\Test.txt"
FileNum = FreeFile

' Create and open file
Open FileName For Output as #FileNum

'Print to file
Print #FileNum, "Hello World!"

' close the file
Close #FileNum

' Good Luck!
0
 

Author Comment

by:VB_SoftMan
ID: 8164372
that' s helpfull. thank you. please explain me more, what each variable is. I know only FileName, but i don' t know that FileNum, FreeFilem, #FileNum used for what. thank you very much, supunr
0
 
LVL 11

Expert Comment

by:supunr
ID: 8164398
Dim FileNum as Long ' this number uniquely identify the open file
Dim FileName as String ' name of the file including the full path

FileName = "C:\Test.txt" ' just hard coded the filename, you can either pass this to the function or ask use to input using common dialog control
FileNum = FreeFile ' FreeFile command automatically finds the next aviable number to uniquly identify the open file

' Create and open file
' Option output create the file always.
' if the file exist, it overwrite the existing file.
' other options are Input and Append
Open FileName For Output as #FileNum

'Print to file
Print #FileNum, "Hello World!"

' close the file
Close #FileNum
0
 

Expert Comment

by:mr_visible
ID: 8164405
Eh, I'm bored so I'll explian each variable for supunr....

the program could alternatevly be written like this:

' Create and open file
Open "C:\Test.text" For Output as #1

'Print to file
Print #1, "Hello World!"

' close the file
Close #1

The "1" is any number (1-255) that you allocate a file in VB. So once you allocate the file to a number, you can use that number to read (input #1, myVariable) or write (print #1, "Hello World!") to the file.

So that you don't have to choose a number for each file you open, VB has a "FreeFile" function which allocates this number for you (any file number which is not being used). Using the freeFile function is a better way of doing it....



0
 

Expert Comment

by:mr_visible
ID: 8164410
Sorry supunr, I wrote my comment a little slow (you got ther first) :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

578 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