Importing External Data
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.
Using the User Interface
To import an external data file using the user interface, follow these steps:
Figure 19.1. The Import dialog box.

Figure 19.2. The Import Spreadsheet Wizard.

CAUTIONIf 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.
Using Code
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.
Importing Database Data Using Code
![]() | 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. |
Listing 19.1 Using the TransferDatabase Method
Sub ImportDatabase()
DoCmd.TransferDatabase _
TransferType:=acImport, _
DatabaseType:="dBASE III", _
DatabaseName:= CurrentProject.Path, _
ObjectType:=acTable, _
Source:="Customer", _
Destination:="tblCustomers", _
StructureOnly:=False
End Sub
NOTE
![]() | This code and all the code in this chapter are located in the CHAP19EX.MDB file on the sample code CD-ROM. |
Importing Text Data Using Code
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.
Listing 19.2 Using the TransferText Method
Sub ImportText()
DoCmd.TransferText _
TransferType:=acImportDelim, _
TableName:="tblCustomerText", _
FileName:=CurrentProject.Path & "\Customer.Txt"
End Sub
Table 19.2 lists the arguments for the TransferText method.
Importing Spreadsheet Data Using Code
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.
Listing 19.3 Using the TransferSpreadsheet Method
Sub ImportSpreadsheet()
DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadsheetType:=acSpreadsheetTypeExcel9, _
TableName:="tblCustomerSpread", _
FileName:=CurrentProject.Path & "\Customer.Xls", _
HasFieldNames:=True
End Sub
Table 19.3 lists the arguments for the TransferSpreadsheet method.