473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

VBA Standard Text File I/O Statements

zmbd
5,501 Expert Mod 4TB
It seems that in VBA some the most fundamentally basic commands for file control are the hardest to find any information for, for example, the basic File operation commands, such as DIR(), CHDRIVE(), CHDIR(), FILECOPY(), NAME(), KILL(), and commands to handle the basic text input/output.

So in this article I want to cover the basics behind the file I/O commands.
FreeFile, Open, Close, LOF, SEEK, Input, Input#, Line Input, Print, Write, Reset, and EOF.

Using these commands along with the string functions can provide a very valuable set of tools without the need to add any library references or late binding object to use the file scripting object. You can archive materials, save settings, offline error logs, and I’m sure that many of us have had the need to import text from a TXT/CSV file. Each of the statements are hyperlinked to the Microsoft Programer's Reference site for more detail... well at the time I wrote this there was useful information there :) ...

FreeFile
intPointer = FreeFile
When a file is opened it needs a pointer. FreeFile allows you to find the very next, lowest, available pointer. It doesn’t increment with use/call if the FreeFile; thus, until you open or close a file the result returned will be the same, so if you open a files with #1, #2, and #3 and call FreeFile it will return #4, if you call it 50 times without opening or closing a file, it will return #4 every time. However, if you close #1, then call FreeFile, it will return #1 and files referenced with #2 and #3 will remain open!

Open
Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]
The basics… you have to open the file before you can use it.
Keep in mind… you open a file for INPUT to the program from the file and OUTPUT from the program to the file.
The FOR modes are: Append, Binary, Input, Output, or Random – Keep in mind, that unless you open as Random, every time you INPUT from the file or OUTPUT/APPEND to the file, the pointer moves forward this is almost the same as a dbOpenForwardOnly recordset. For more information about using the Random mode How to Work with Random Access Files
The lock modes are: Shared, Lock Read, Lock Write, and Lock Read Write
Reclength, I rarely use and has to do with either the bytes or characters buffered depending on how the mode used to open the file; however, say the data is in the first 100 characters of a 10000 character file, why open the entire thing.
So the basic syntax to open the file would be
Open "C:\in.txt" For Input As intPointer to create/overwrite the file
Open "C:\in.txt" For Append As intPointer to append to an existing file.

Close
CLOSE filenumberlist
The basics, if you open it, close it. You also need to close a file between modes, so to go from input to output or random and lock types you need to first close the file then re-open the file with the correct settings.

LOF()
intChars = LOF [#]filenumber
Returns the number of Characters in the open file.
intX=LOF( 1)

SEEK
SEEK [#]filenumber, position
Sets the position within the opened file for the next read/write operation. Be careful here or you will overrun the file length and error out - and yes, you can move the pointer back to the start of the file using this method (unlike a dbOpenReadForwardOnly recordset)
Seek #1, NextChar

Input
strImport = INPUT(#Toimport, [#]filenumber )
The Input function will import the number of specified characters at a time into Access. This is probably of most use when you have a record of fixed width or length (i.e. you have set the Len argument of the Open statement). Once again, be careful of the file length or you will overrun the file and error out
For example this would import the entire file that you could then parse, if you have the memory and you’re reading across a slow network this could be more useful.
strImportFile = input(len([#]filenumber), [#]filenumber )

Line Input #FileIndex
Line Input #filenumber, varname
Does what it says, reads the entire line into the variable and you split as needed.
Line Input #1, strReadLineIn
(Note: The syntax here is important, the "#" sign must be part of the code so Line Input #intIndex, strReadLineIn will work however, Line Input intIndex, strReadLineIn will error)

Print #
Print #filenumber, [outputlist]
Well we need a method to put data into the file. This method sends the information to the file pretty much as is, appending a <cr><lf> at the end of the output. It doesn’t wrap extra characters around things so it’s a better choice for exporting to other programs. Therefore, if you need delimitation in the file, you need to provide it in the string format. The other quirk is that Data classed as date is output as the system short date and it will insert spaces between the list “fields.”
There’s a series of arguments that can be included to help with output formatting (see the link).
Sending a print# to the file without anything inserts an empty, zero length, string (blank line); however, If there is a Null valued variable, it will insert a Null character in the line and then return.
There’s an example in the link, the most basic is:
Expand|Select|Wrap|Line Numbers
  1. ’Simple single line:
  2. Print #1, “This is a single line to the file” ‘you get exactly as shown
  3. ‘print a line of information
  4. Print #1,  “a”;”b”;”c”;”d” ‘you get “abcd” on the line
  5. ‘two zones
  6. Print #1, “zone1”, “zone 2” ‘ you get “zone 1             zone2” however, if zone1 text is larger then things push
  7. ‘two zones – better formatting
  8. Print #1, “zone1”; tab ; “zone 2” ‘ same as above, but with the tab character that you can then use to set the starting column such as tab(10)
  9. ‘A trailing “;” semicolon will hold the line for the next print# operation.
  10. Print #1,  “a”;”b”;”c”;”d”;  ‘note the trailing ;
  11. Print #1,  “e”;”f”;”g”;”h” ‘you get “abcdefgh” on the line and a <cr><lf>
  12.  
> - <
The two following work in conjunction with each other and wrap things in double quotes. Be aware, that anything with embedded double quotes will have issues with these two methods/statements as these will create a new “element” within the “record”/Line.

Write #
Write #filenumber, [outputlist]
Unlike the Print # statement: This statement re-formats and translates a lot things, ignores the local settings, inserts commas between items and quotation marks around strings as they are written to the file and adds the <cr><lf>. The link has a good example of how and what you get. I use this rarely and only for a few “deep” archives as there has to be some care taken as to how the data is formatted and the embedded quotes can make a mess of things.

Input#
Input #filenumber, varlist
Note the variable list here… this works with the Write# statement… and from the manual: “it is a Required. Comma-delimited list of variables that are assigned values read from the file — can't be an array or object variable. However, variables that describe an element of an array or user-defined type may be used.” These variables are also type castable so that a date can be assigned to a date, true/false to a Boolean, etc…
This isn’t really the most useful of the input methods; however, I’ve used it upon occasions when I’ve written files using the WRITE# method… usually for deep archives where if I ever really need the data it’s a truly bad day (week, month).

This is a handy command:
Reset.
And that is the code, place Reset on a line and execute… and EVERY file you currently have open is flushed, saved, and closed – period, no prompt, just done. It’s a nice thing to have in an error event handler or in the cleanup section of the code.

Ahhh Yes, one more,

EOF
You need a means of testing for the end of file. So to borrow the code from the above link as it really cannot get any more simple and generic than this for an example:
Expand|Select|Wrap|Line Numbers
  1.  Dim InputData
  2. Open "MYFILE" For Input As #1    ' Open file for input.
  3. Do While Not EOF(1)    ' Check for end of file.
  4. Line Input #1, InputData    ' Read line of data.
  5. Debug.Print InputData    ' Print to the Immediate window.
  6. Loop
  7. Close #1    ' Close file. 
There are other text file IO commands such as GET and PUT for simple record handling. Ah, Yes, I did very old school “database” handling with this stuff back in the day… then SQL and real database engines became available to us little people (and much rejoicing filled the land).

Once again, unless opened for random, every INPUT/OUTPUT action on the file will generally move the index forward in the open file unless a SEEK() is performed.

There are other basic VBA statements that can also save you some effort, most of these we use frequently; however, a few are less so:
Office Visual Basic Language Reference>Statements
some of these have been retained only for backwards compatibility (“Error”) and others such as “Gosub” have fallen out of favor and considered poor programing technique. Another reference to keep handy are the basic functions for VBA: Office Visual Basic Language Reference>Functions

Most of this information is common knowledge to us old school BASIC/FORTRAN/COBOL programmers. I’ve borrowed an example or two from various places altering the variables, however, this is like a C-scale in music, it’s a C-scale and it’s in the public domain so to speak.
Dec 18 '13 #1
3 75845
jimatqsi
1,271 Expert 1GB
Wow, very nice to have all this information in one place. Thank you!
Apr 19 '14 #2
Excellent post, thanks so much.
Do you know what vba uses to define the end of the file? As it uses CR or CRLF to work out the end of line, is there a similar character it uses to define end of file?
Apr 18 '16 #3
zmbd
5,501 Expert Mod 4TB
Databoe:
Each of the statements (in blue) are hyper-linked to MS programmer's reference for more information.

In the case of Line Input #Fileindex it is a case of both-and not either-or; however, with the CRLF combination the CRLF is usually dropped from the string where with just the CR the return is kept. I have had occasions where this hasn't held true so one must usually inspect the string in code before using.

As for the simple Input you have to parse the string yourself once fetched to the variable.

Hope that helps.

For deeper discussion we do ask that you start a new thread in the main Q&A section ... more people will see the question and it helps to keep the articles on topic.


-z
Apr 20 '16 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
1
by: redneck_kiwi | last post by:
I am attempting to import data from a text file. There are many records to be imported and the raw file format is: "1 |010-5502-502 |16H1-7-3 |CONTACT, ELECTRICAL, 7/8in. ...
2
by: Ron | last post by:
Hi All Okay, I've got a report that runs perfectly. Designed originally with the report wizard and enhanced with about 8 subreports and other stuff. Works great. However, I also now need to...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
5
by: Amos Soma | last post by:
Has anyone written a utility (preferrably in C#) that takes a text file or string and extracts any SQL statements (e.g., Select, Insert, Update, Delete, Alter etc.) contained in the file or...
8
by: Scott | last post by:
I wish to create a text document (to the users desktop on any machine) which outputs to a standard text file with information obtained from there use of my program... Some of the text will be fixed...
2
by: samithajai | last post by:
1)what is a non standard text file for input or output in c? 2)I want to read a simple xml file (without any namespaces and dtd) in c and get its contents How should i do it? thanks
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
2
by: finked1973 | last post by:
Hi All I have a piece of code in my application that is giving me a funny result. I am reading a text file and based on it's first word on the line I have a bunch of IF statements that then assign...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.