| DatabaseLessons.com |
'Serving the Microsoft® Access |
|
|
Using Text Files - Part 1In the MS Access database projects that I work on, I often have to either import from or export to text files, also referred to as ASCII files or flat files. Let's start with a look at the menu methods available to you. Under the File menu, there is an entry named "Get External Data", and then "Import...". The extra "..." indicates that when you choose that entry, a dialogue box will appear on the screen to ask you further details. The next step is to change the "Files of type:" entry to show "Text Files (*.txt;*.csv;*.tab;*.asc)". It is my habit to use the .txt extension for all my text files. Thus, they will appear under that choice and I will then look for the appropriate file in my folder structure, highlight it and then click the import button. Generally speaking, your text files should either have their "fields" separated by a delimiter (usually a comma or a tab, but can also be a space, semicolon, or any other chosen character), or they may be in fixed widths where each field always take the exact same space in each record. Let's look at the delimited style first. The fields will be automatically sensed as per the delimiters. Subsequent dialogue box screens will ask you to
As I am almost always importing temporarily, and will be deleting the new table later, I use the defaults for most of the choices. If your data is in fixed format, the main difference in the above is that you must verify the field boundaries in the data. If your data is all filled in (ie. no spaces) Access will not be able to guess where the fields end. Note that the dialogue box that you get in this case has instructions on how to create, delete, or move a field break line. A little experimentation will get you sucessfully on your way quickly. Finally, if Access encountered any problems importing your data, it will create an Import Errors table. For example, if your desired imported data table name is "ABC", then the error table (if needed) will be "ABC_ImportErrors". Simply open this table and use it to determine what went wrong. Then delete the two new table and try the import again. The most common problem is when you use defaults, and Access decides the field is numeric, but some of the data has alphabetic characters in that field. Just over ride the numeric choice with a string choice and the import will succeed. Part 2 of this series will examine basic exporting. (article continues after sponsor spot) NOTE: All the VBA code segments on the Database Lessons site assume that you have DAO references active. If you are not sure what this means, and you are using Microsoft Access 2000 or higher, click here. Happy Coding |
|
|
© 2006, 2007, 2008 Richard W. Killey. All Rights Reserved. |