views:

340

answers:

2

MS Access has a passable text file import wizard. (File -> Get External Data -> Import.) Unfortunately, text columns (fields) are always imported with 255-character size, regardless of the actual data found in the input file.

How can I reduce these 255-character Text columns to a reasonable size without manually doing a bunch of "SELECT Max(Len(_column_)) FROM _data_" queries?

I already have a solution and I will post it below.

+1  A: 

I have created a tool that will scan through an imported table after the fact, report the actual minimum and maximum sizes found in Text columns, and allow you to change the column sizes all at once:

http://www.glump.net/software/microsoft_access_table_optimizer

This tool does not affect the MS Access import wizard, nor does it replace it. So, if you're importing a huge number of records or columns with Text values, you might run into space issues before you get to the point where you can run my tool. YMMV.

The tool is a module of VBA code that 1) creates a report of all columns in a given table and minimum and maximum lengths of values and 2) after you edit the report table, resizes columns with your specified new size.

Brendan Kidwell
+2  A: 

Is there any reason to do this? Reducing the field size only reduces the amount of text the field can store, not the amount of space the file takes on disk.

Remou
If any of the fields are indexed, it could make a significant difference. I usually do imports in a staging table and then append to the real table, so I don't have to muck around with this kind of thing.
David-W-Fenton
Well... If you're importing data from an unknown source you still need to KNOW THE SIZE to make the column in the target table after the staging table. When your staging table is varchar(255), varchar(255), varchar(255)... it's not very helpful.
Brendan Kidwell