导入文本文件时如何指定字段类型?(一)

来源:互联网 发布:什么不亦说乎 编辑:程序博客网 时间:2024/05/18 21:48
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

导入文本文件时如何指定字段类型?(一)

 

问题:

我有一个文本文件需要导入  ,但是文本文件中有一列数据原本是文本,但是导入后自动变成了“双精度”类型,我该如何让各个字段按我需要的数据类型生成哪?
如何让 ACCESS 按照我规定的构架、规格从文本文件、XLS文档中导入数据?
 





方法一:

Access 本身就有导入导出向导,里面得高级选项中就可以自定义需要导入数据得详细规格。


操作步骤如下:

菜单 -> 文件 -> 获取外部数据 -> 导入(或者是“链接表”) -> 选择你需要得格式得文件 -> 高级


菜单 -> 文件 -> 获取外部数据 -> 导入(或者是“链接表”)
图片如下:
screen.width-400)this.width=screen.width-400"border=0>

选择你需要得格式得文件
图片如下:
screen.width-400)this.width=screen.width-400"border=0>

点击“高级”按钮
图片如下:
screen.width-400)this.width=screen.width-400"border=0>

在“导入规格”中定义字段规格,定义完成后按“保存”按钮
图片如下:
screen.width-400)this.width=screen.width-400"border=0>

选择合适得规格名
图片如下:
screen.width-400)this.width=screen.width-400"border=0>

定义完成后得字段规格存储在 MSysIMEXColumns 以及 MSysIMEXSpecs 表中
图片如下:
screen.width-400)this.width=screen.width-400"border=0>

完成上述操作后,以后就不用再重新定义了,可以使用以下语句链接或者导入文本文件
DoCmd.TransferText acLinkDelim, "list1 导入规格", "list1", "C:/list1.txt"
DoCmd.TransferText acImportDelim, "list1 导入规格", "list1", "C:/list1.txt"


关于 TransferText 方法请参考以下资料(摘自 Access XP 帮助)



TransferText 方法

在 Visual Basic 中,TransferText 方法执行 TransferText 操作。

expression.TransferText(TransferType, SpecificationName, TableName, FileName, HasFieldNames, TableName, CodePage)
expression   必需。返回“Applies To”列表中的一个对象的表达式。

TransferType  可选 AcTextTransferType。

AcTextTransferType 可以是下列 AcTextTransferType 常量之一: 
acExportDelim 
acExportFixed 
acExportHTML 
acExportMerge 
acImportDelim 默认 
acImportFixed 
acImportHTML 
acLinkDelim 
acLinkFixed 
acLinkHTML 
如果将该参数留空,则采用默认常量 (acImportDelim)。

注意   可以链接到文本文件或 HTML 文件中的数据上,但数据在 Microsoft Access 中是只读的。

在 Microsoft Access 项目(Microsoft Access 项目:与 Microsoft SQL Server 数据库连接且用于创建客户/服务器应用程序的 Access 文件。项目文件中不包含任何数据或基于数据定义的对象(如表或视图)。) (.adp) 中仅支持 acImportDelim、acImportFixed、acExportDelim、acExportFixed 或 acExportMerge 转换类型。
 

SpecificationName  可选 Variant 型。字符串表达式(字符串表达式:任一求值为一列连续字符的表达式。表达式的元素可以包括:返回字符串或字符串 Variant (VarType 8) 的函数;字符串字面值、常量、变量或 Variant。),表示在当前数据库中创建并保存的导入或导出规格的名称。对于固定长度的文本文件, 必须指定参数或使用 schema.ini 文件,该文件还必须保存在导入、链接或导出的文本文件的同一个文件夹中。若要创建一个方案文件, 可使用文本导入/导出向导创建此文件。对于分隔的文本文件和 Microsoft  邮件合并数据文件,可以将该参数留空,以便选择默认的导入/导出规格。1
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>