========================================================
CONVERTING AND IMPORTING FILES INTO MICROSOFT ACCESS 2.0
========================================================



OVERVIEW

This article discusses how to convert files into a format which can
be imported into Microsoft Access 2.0.  We'll deal primarily with 
changing a non-standard file into a text file and importing it.
We WON'T discuss importing database tables or spreadsheets.  Please
refer to chapter nine of the Microsoft Access User's Guide for
information on these topics.

If you're planning a conversion and/or importation, this article can
be used as a check list of issues for your consideration.
 


WHY CONVERT & IMPORT THAT OLD NON-STANDARD FILE ?

The most compelling reason is that you need it to run your business. 
The file may be customer information, product information or financial
records but it's necessary.  Critical information needs to be converted
and integrated with your Microsoft Access applications as soon as
possible.

Accessibility is another important consideration.  You may want the file
in Microsoft Access so you can retire your old system without losing
the information from your old files.  You can convert the file, write 
the forms and reports in Microsoft Access, and retire your old system.

Increased productivity is another possible reason.  If your old system
is hard to maintain or adding new features is difficult, using 
Microsoft Access could increase your productivity.



DO I NEED TO CONVERT THIS FILE ?

Microsoft Access supports the importation of the following formats:

                 Microsoft Access
                 Text (Delimited)
                 Text (Fixed Width)
                 Microsoft Excel 2.0-4.0
                 Microsoft Excel 5.0
                 Lotus (WKS)
                 Lotus (WK1)
                 Lotus (WK3)
                 Paradox 3.X
                 Paradox 4.X
                 FoxPro 2.5
                 FoxPro 2.6
                 FoxPro 2.0
                 dBase III
                 dBase IV

and SQL data sources.  If you're importing a file in one of these
formats, a conversion is not necessary.

Please refer to chapter nine of the Microsoft Access User's Guide and
Help for more information.




TEXT FILES SUPPORTED BY MICROSOFT ACCESS 2.0

Two types of text files can be imported:  delimited text files and
fixed width text files.  In each type of text file, a new line is
imported as a new record.  In other words, a text file with 99 lines
will create 99 records when imported.

Delimited text files have field separators indicating where one 
field ends and the next one begins.  The default field separator is
a comma.  They also have a text delimiter such as a double quotation
mark in case the field separator occurs within a text field.  For
example:
            "Sage Database, Inc.","Houston, TX" 

is correctly processed as two text fields instead of four text fields
because we're using a text delimiter.

Fixed width text files have fields which always start in the same
location and occupy the same amount of space.  These text files require
more extensive specification work before they can be imported.  A fixed
width text file could look like this:

         Sage Database, Inc.         Houston, TX           77227
         BayMart                     Rochester, New York   14612                



PREPARATION BEFORE IMPORTATION

Design the table before you import the file!

The most important step is designing your table to accept the file
you're importing.  The design of your table should meet the following
criteria:
              (1) include all the fields from the file in your
                  table design;
              (2) insure all the fields are the correct type;
              (3) insure all the fields are large enough;
              (4) use validation rules when appropriate;
              (5) use the required property when appropriate.

Designing a table meeting these criteria facilitates the importation
as well as flagging unexpected errors (see VERIFICATION OF PROCESS).

In the remainder of this article, the word "file" will denote the file
you're importing and the word "table" will denote the Microsoft Access
table you're building from the file.



WORKING WITH DELIMITED TEXT FILES

Let's assume you have a delimited text file ready to be imported.
What actions do you take and how does Microsoft Access respond?

     Your action                   Access response
     -----------                   ---------------
(1)  Open the database             Database window opened

(2)  Choose import from the        The "Import" dialog box is opened
     File menu

(3)  Select Text (Delimited)       The "Select File" dialog box is
     and click OK                  opened

(4)  Choose the file you want      The "Import Text Options" dialog box
     to import and click import    is opened

(5)  Since you've designed the table, select "Append to Existing Table"
     and use that table.  For this scenario, we'll assume your delimited
     text file conforms with the default options on the "Import Text 
     Options" screen.

(6)  Click Save As... to save      Access saves your specification
     for use in Access Basic 
     code      

(7)  Click OK                      The file is imported and a message
                                   box gives you the record count and
                                   alerts you to any errors 



WORKING WITH FIXED WIDTH TEXT FILES
 
Let's assume you have a delimited text file ready to be imported.
What actions do you take and how does Microsoft Access respond?

     Your action                   Access response
     -----------                   ---------------
(1)  Open the database             Database window opened

(2)  Choose import from the        The "Import" dialog box is opened
     File menu

(3)  Select Text (Fixed Width)     The "Select File" dialog box is
     and click OK                  opened

(4)  Choose the file you want      The "Import Text Options" dialog box
     to import and click import    is opened

(5)  Since you've designed the table, select "Append to Existing Table"
     and use that table. 

(6)  Click Edit Specs...           The "Import/Export Setup" dialog box  
                                   is opened 
                                   
     Fixed width text files require specifications before you can import
     them.  They generally do not have a text delimiter and the field
     separator is usually a space.

     You'll need to fill out the field information section.  If your
     table structure is similar to the file layout, click the "Fill 
     Specifications Grid from Table" button.  Access will build    
     field information exactly matching your table.  You'll need to
     change the field information to get an EXACT match with your file.

(7)  Click Save As... to save      Access saves your specification
     for use in Access Basic 
     code      

(8)  Click OK                      The file is imported and a message
                                   box gives you the record count and
                                   alerts you to any errors 



ADDITIONAL ISSUES

In the preceding two sections, we assumed your file layout matched
your table definition and the default option settings in order to
give you a brief overview of the importation process.  Now we'll
consider additional issues related to importing a file.

The option "First Row Contains Field Names" is one you'll probably
use occasionally.  If your file contains the same fields as your
table but in a different sequence, you can list the field names of 
table in the order they appear in the file as the first row of the
file.

For example, the fields in your table are "Actual Date", "Time" and 
"Location" but your file has date, location and time.  You would add
this line to a delimited text file as the first row of your file to 
import it correctly:

Actual Date,Location,Time

The field names must exactly match those in your table.  Microsoft
Access will import the fields into the correct location in the table.



VERIFICATION OF PROCESS

This verification step insures that no data transfer errors occurred
during importation.  Microsoft Access alerts you to errors and creates
an import errors table listing the error, the name of the field in 
error, and the row in the source file causing the error.

Review the errors!  Some errors may be unavoidable due to conflicts
between the design of your table and the file.  For example, if your
table limits an address line to forty characters and the file has
fifty characters, you'll get FIELD TRUNCATION errors.  If your file  
has no value in a field where the property Required=Yes, you'll get
NULL IN REQUIRED FIELD errors.

You need to decide if this is acceptable or if your table design needs 
to be changed.  If you don't change your table design, you'll need to
enter valid values in your table to compensate for the import errors.

Other import errors are more serious and need to be resolved before
you can use the table.

TYPE CONVERSION FAILURE means the format of a field in the input file is
incorrect and can't be converted.  If this error occurred on each record
of the input file, either the file specification or table design is 
incorrect.  Correct the problem and import the file again.  If this 
error occurs infrequently, you probably have a few bad fields in the 
input file.  You should correct these fields and import the file again.

KEY VIOLATION means you've got a duplicate key in the input file.
Change the duplicate key to a unique key and import the file again.

VALIDATION RULE FAILURE means Microsoft Access was able to convert 
the field but it failed the validation rule specified in the table's
ValidationRule property.  Either the table's ValidationRule property
is too restrictive or you have bad fields in the file.  Correct the 
problem and import the file again. 

UNPARSABLE RECORD means the record has a text field with the text
delimiter embedded in it.  The text delimiter must be repeated twice
for the record to be parsable.  For example:

   "TEXT FIELD ERROR MAKES "UNPARSABLE" RECORD" causes an error
   "TEXT FIELD AVOIDS AN ""UNPARSABLE"" RECORD" is OK                



VERIFICATION OF CONTENT

This verification step insures that correct VALUES were imported.

It is possible for the text file to be imported without any errors
occurring during importation and have incorrect values because of
an error in the import specification.

You should compare a report produced from the original data in the
original system to the same report produced by Microsoft Access.

If possible, also use summary information and field verification   
tests.  Summary information such as record counts and amount totals
can show small errors which can escape a visual inspection.  Field
verification tests such as checking date ranges and editting fields
for valid value will also finds errors.



AUTOMATING THE IMPORT

The TransferText command allows you to automate an import with Access 
Basic.  The format of the command is:

DoCmd TransferText [transfertype][,specificationname],tablename,filename
                   [,hasfieldnames] 

Only tablename and filename are always required. Transfertype 
default value is A_IMPORTDELIM, specificationname is only required
for fixed width text files but is recommended for all files, and
hasfieldnames default value is False.

Please select help on "import data:  using macros/actions" and 
select topic "TransferText Action" for more detailed information
on this command.



SAMPLE CODE

This Access Basic code can be attached to a command button.  It will
automate the importation of an existing file into an existing table.

In this example, we're importing a delimited file (transfertype =
A_IMPORTDELIM) from a text file (filename = "c:\file.txt") into a
Microsoft Access table (tablename = "Tbl") using specifications
(specificationname = "Spec") and the first row of the file does NOT
contain filename (hasfieldnames = False).

Option Compare Database   'Use database order for string comparisons

Sub cmdImport_Click ()
On Error GoTo Err_cmdImport_Click

   DoCmd TransferText A_IMPORTDELIM, "Spec", "Tbl", "c:\file.txt",False

Exit_cmdImport_Click:
   Exit Sub

Err_cmdImport_Click:
   MsgBox Error$
   Resume Exit_cmdImport_Click

End Sub



HOW DO I CONVERT THIS FILE SO IT CAN BE IMPORTED ?

You can use several methods to convert a file.  They range from export-
ing data to programatically restructuring files.

EXPORTING DATA from your old system in a format Microsoft Access will
import is the most straight forward method.  However, if your old system 
does not support exportation, you will not be able to use this method.

DATA CONVERSION TOOLS can convert a wide range of formats.  If you're
regularly converting data between various formats, this may be the
most cost-effective approach.

PROGRAMATICALLY RESTRUCTURING files may be necessary if you want change
your old system from a "flat file" to a relational database structure.
For example, your old system contains a file with product and supplier
information.  The supplier information is repeated often since you
order many products from a single supplier.  

A normalized database would have the supplier information and product 
information in separate tables with a one-to-many relationship
between supplier and products.  In order to achieve this structure,
your "flat file" would need to be programatically restructured into
two tables before importation into Microsoft Access.



PREPARED BY Sage Database, Inc.  Please contact us by e-mail for more
information on our products and services:

                   AOL:  RPWatSAGE

                   CIS:  73750,1243

              Internet:  RPWatSAGE@aol.com

                  Mail:  P. O. Box 22306
                         Houston, TX 77227-2306

Microsoft and Microsoft Access are registered trademarks of Microsoft
Corporation in the USA and other countries.
