Visual Basic Code of the Week (COTW)
Issue #66
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1998 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Free Software

Enter our monthly contest at for the chance to win FREE software for your development needs.

Download an evaluation of PowerFindPro at

Late breaking information

Back issues are now available in Spanish for subscribers of Code of the Week at (free trial members will need to subscribe to access them).

Check out for an exciting subscription offer. You do not want to miss this one.

In this Issue

This issue is the second part to last week's issue. It was designed for VB 4.0 32-bit and up and describes how to import a comma separated value (CSV) file. It uses DAO database access methods on an Access database. Questions? Email us at

The complete source code appears near the end of this issue. Be sure not to miss it!


Last week we discussed how to import any CSV file. The one problem with last week's routine is that if the first record in the CSV file did not contain field names, DAO will just assign the names F1, F2, F3, F4, etc. This week we show how to rename any columns you want after performing the import. We will assume you have read the content of issue #65. If you did not receive it, you can view it at


Public Sub RenameColumn(sOldColumnName As String, sNewColumnName As String)
Public Function ImportDataAndRename() As Long

RenameColumn allows you to specify a column from the import file (sOldColumnName) and the name (sNewColumnName) that this column should be after importing the data. The ImportDataAndRename function will use this information to change the column names in the imported table. You will need to call RenameColumn once for each column you would like to rename. Keep in mind that if your CSV file does not contain the field names as the first row, the import table will have columns called F1, F2, F3, F4, and so on starting with the first column. See the sample code for details on using RenameColumn.

ImportDataAndRename is a wrapper around the ImportData routine discussed last week. It will first call the ImportData routine and then process the OldColumnNames and NewColumnNames collections.

If the table already exists in the destination database it will remove it and overwrite it. Depending on your application you might want to change this behavior. You might want to raise an error and give the end user a choice to overwrite the table or not.


ImportDataAndRename will return the number of records that it imported. Error 457 will be returned if you try to call RenameColumn with the same column name more than once. Error 3265 will be returned if you call ImportDataAndRename and specify an non-existent column name as the sOldColumnName parameter in RenameColumn (to put another way, if you try to rename a column that does not exist).

Sample Usage

Below is an example of how this class works. It is assumed e:\temp contains the file freesub.txt to import. It also assumes the database e:\temp\importtest.mdb exists already.

    Dim Import As New cImportCSV
    Dim lRecs As Long

    Import.CSVFilePath = "e:\temp"
    Import.CSVFilename = "freesub.txt"
    Import.DatabaseName = "e:\temp\importtest.mdb"
    Import.NewTableName = "newimport"

    ' will rename column F1 to email, F11 to firstname, and F2 to lastname
    Import.RenameColumn "F1", "email"
    Import.RenameColumn "F11", "firstname"
    Import.RenameColumn "F2", "lastname"  ' will return err=457 if the names were
                                            ' already used.
    lRecs = import.ImportDataAndRename      ' will return err=3265 if you specified
                                            ' an illegal (doesn't exist) old column name

    MsgBox lRecs & " records imported."

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945