The information in this article applies to:
SUMMARYBy itself, a file consists of nothing more than a series of related bytes located on disks. When your application accesses a file, it must make assumptions about what the bytes are supposed to represent (integers, strings, or other data types). Microsoft Excel Visual Basic for Applications provides functions and statements that allow you to process the file based on these assumptions. By processing files, your application can create, manipulate, and store large amounts of data, access several sets of data at once, and share data with other applications. Binary access allows you to use files to store data however you want; there are no assumptions made about data type or requirements for standard record length. However, you must know precisely how the data is written to the file in order to retrieve it correctly. MORE INFORMATION
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
Note that LName and FName are strings, which is a variable-length data type. Age is an integer, which is a 2-byte data type. For more information about the bytes required by data types, see the online Help topic, "Data Type Summary." Advantages of Binary Access Files
Disadvantage of Binary Access Files
Writing to Files Opened for Binary AccessBecause records with binary access can be of variable length, it is necessary to actually store information about the size of each field and record so that it can be read successfully. A good way to accomplish this is to store an integer with each string to indicate the length of the string. The following is an example of creating such a file:
When the WriteBinary macro is run, it will create a file called BINARY.TXT. The two records in this example take up 34 bytes (as opposed to the 44 bytes required by the same data with random access). Keep in mind that when opening this file in a text editor, such as Notepad, the file will not be readable. It is a binary file, not a text file. A trade-off in using variable-length field and binary access instead of fixed-length fields and random access is that the entire record could be written with a single function call using random-access. While binary access provides greater flexibility, it also requires more code to handle I/O operations. Reading Files Opened for Binary AccessThe Get statement reads a number of bytes equal to the bytes required for the variable that is used. When you use Get with a variable-length string, the number of bytes read from the file equals the current length of the string. To temporarily set the length of a variable-length string, you can use the STRING$ function to set the variable equal to a specific number of blanks, or spaces.The following example reads a file like the one created with the WriteBinary macro:
Additional query words:
Keywords : kbcode kbprg xlloadsave xlvbahowto xlvbainfo |
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |