Text Files

The TransferText method allows you to import from, link to, and export to text files. It has the following syntax.

DoCmd.TransferText(TransferType, SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)

As you would expect, various parameters can be used to specify how the method should execute. These parameters are similar to the TransferDatabase and TransferSpreadsheet methods you have already seen. The following table explains the use of each parameter.

EÏI Microsoft Excel - Employees

j^J Eile Edit View

insert Format Tools Data Window

¡Manage Help

_ S

X

i J ^ ^ _j

äÜ ^ '

a ¿v(

T 1 S -A: ^ -

âiljlftm =

I .J

■-J> !

6

iä iM1 ßep

y with Changes...

T

A1

* fx EmployeelD

A

3

c

D

E

F

G -

A

1

Employee

LastName

FirstName I ille

Title Of Col

Birth Date

HireDati

£

1

Davoliü

Nancy

Sales Re[: Ms.

12/8/1368

5/1/

3

I

Fuller

Andrew

Vice Presi Dr.

2/19/1352

3/1 Ai

=

A

3

Leverllng

Janet

Sales Rep Ms.

3/30/1 363

Ai 1/

5

A

Peacock

Margaret

Sales Rep Mrs.

3/13/1353

5/3/.

S

5

Buchanan

Steven

Sales Mar Mr

3/4/1355

10/17/

7

6 Suyarna

Michael

Sales F^ef: Mr.

7/2/1 363

10/17/

3

7

King

Robert

Sales Ret: Mr.

5/23/1 36G

1/2/

3

3

Callahan

Laura

Inside Sali Ms.

1/3/1358

3/5/

10

S

Düdswortf Anne

Sales Rep Ms.

7/2/1363

11/15/

V

1 .f

► n \tblEmpïoyees/

j

<

5 1

j Draw -j i Auto Shapes- \ \ □ O ^J O JJ

-

Ready

Figure 7.2

Parameter

Description

TransferType

Type of transfer to be performed. The default is acImportDelim. See the help documentation for a complete list and explanation.

SpecificationName

Name of import or export specification you have saved in the current database. This argument can be left blank for delimited text files.

TableName

String expression that contains a table name you want to work with or a query you want to export.

FileName

Filename and path of the text file you want to work with.

HasFieldNames

Use True to use the contents of first row of the spreadsheet as field names and False to treat the first row as data. False is the default.

HTMLTableName

Use with acImportHTML or acLinkHTML. Name of table or list in HTML file you want to work with. If blank, the first one is assumed.

CodePage

Long value indicating the character set of the code page.

Next, we'll jump right into importing data from a text file.

Try It Out Importing Data from a Text File

Now, you import data from a text file into a new table, called tblEmails, in your Access database. 1. Create a text file as shown in Figure 7.3 and save it in C:\temp.

emails - Notepad

File Edit Format View Help

"First Name", "Last Name", "Email" "Denise", "Gosnell", "denisegosneliSyahoo. "John", "Doe", ";[email protected] com" "Jane", "Doe", "[email protected]"

com"

2. Add the following procedure to the module in your database.

Sub TestTransferText()

DoCmd.TransferText acImportDelim, , _

"tblEmails", "C:\Temp\emails.txt", True

End Sub

3. Run the procedure from the Immediate Window in Visual Basic Editor.

4. Return to the database window and you should see a screen similar to that shown in Figure 7.4.

0 0

Post a comment