The process of importing external data is quite simple. You can import external data by using the user interface or by using VBA code. If you are planning to import the data only once or twice, you should use the user interface. If you are importing data on a regular basis (for example, from a downloaded mainframe file), you should write code that accomplishes the task transparently to the user.
To import an external data file using the user interface, follow these steps:
Right-click anywhere within the database window.
Choose Import (or choose File, Get External Data, Import). The Import dialog box appears, as shown in Figure 19.1.
From the Files of Type drop-down list, select the type of file you are importing.
Select the file you want to import and click Import.
Depending on the type of file you select, the import process finishes, or you see additional dialog boxes. If you select Excel Spreadsheet, for example, the Import Spreadsheet Wizard appears, as shown in Figure 19.2. This wizard walks you through the process of importing spreadsheet data.
CAUTION
If you find that you can't bring a large (4MB5MB) text file directly into an Access database, change the text file into an Excel spreadsheet first and then import that file.
The DoCmd object has three methods that assist you with importing external data: TransferDatabase, TransferText, and TransferSpreadsheet, each of which I cover in this section.
You use the TransferDatabase method of the DoCmd object to import data from a database such as FoxPro, dBASE, Paradox, or another Access database. Listing 19.1, included in basImport, shows an example that uses the TransferDatabase method. |
NOTE
This code and all the code in this chapter are located in the CHAP19EX.MDB file on the sample code CD-ROM. |
Table 19.1 lists the arguments for the TransferDatabase method.
You use the TransferText method of the DoCmd object to import text from a text file. Listing 19.2 shows an example of this method.
Table 19.2 lists the arguments for the TransferText method.
You use the TransferSpreadsheet method of the DoCmd object to import data from a spreadsheet file. Listing 19.3 shows an example that uses the TransferSpreadsheet method.
Table 19.3 lists the arguments for the TransferSpreadsheet method.
Argument | Specifies |
---|---|
TransferType | Type of transfer you want to make. |
SpreadsheetType | Type of spreadsheet to import from. The default is Excel 3.0. |
TableName | Name of the Access table that will receive the imported data. |
FileName | Name of the spreadsheet file to import from. |
HasFieldNames | Whether the first row of the spreadsheet contains field headings. |
Range |