?
Solved

How to create text file with VB?

Posted on 2003-03-18
5
Medium Priority
?
430 Views
Last Modified: 2013-12-26
what are command in vb that use for create text file?
0
Comment
Question by:VB_SoftMan
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

762 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