如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

huangapple go评论70阅读模式
英文:

How to import txt file to Microsoft Access with fields as text, not numeric

问题

I'm trying to import a txt file into my Microsoft Access database but data is being treated as numeric instead of text.

My source file (named sourcefile.txt) is like this:

00120230200001083300091959212311
00120230200001220500220559212311
00120230200240082700083259212311
00120230200240220600224059212311
00120230205001224100234959212311
00120230300001190700193359212311

Around 80,000 rows of data, no fields or delimitation, all rows same length (32 chars). I want to import that data to a table named T_RAW_SWD with just 2 fields:

  1. swd is the first field and has been set as text with 255 limit characters
  2. id_swd is autonumeric and is the primary key

So the idea is to import all those data rows as text into field swd. I've tried just reading data using a query like this:

SELECT F1 AS swd FROM [Text;FMT=Delimited;HDR=No;Database=C:\Temp;IMEX=1].filesource.txt;

This code loads the data but as a number, not text. I thought using IMEX=1 would make the trick but no. Here's the output:

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

Desired output:

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

I've read some sources but could not figure out a solution. Also, I've tagged the question with VBA because I tried with different macros based on some sources like these ones:

https://stackoverflow.com/questions/62820045/excel-import-text-file-using-vba-in-access

https://stackoverflow.com/questions/58174825/how-to-read-txt-files-and-import-them-into-access-table

https://stackoverflow.com/questions/32390960/vba-code-import-a-text-file-into-an-access-table-with-condition

https://stackoverflow.com/questions/12137085/changing-connection-string-in-excel-messes-up-column-data-types/12145949#12145949

DoCmd.TransferText method (Access)

ImportExportSpecification object (Access)

The different codes I've tested import the data also as numeric. I've managed to load data into queries or tables but as numeric always. I'm totally stuck about what to do to read data as text so I don't lose any info.

If I manually import the data with the wizard it works properly, but there are several files and this task will be daily so I'm trying to make it faster with SQL code invoked from VBA or VBA itself.

Also tried first creating manually with the wizard an import routine and call it from DoCmd.TransferText using acImportFixed with no success.

Any ideas of what I'm doing wrong here? I got no errors on importing the data through code, but I need it as text, not numeric.

By the way, I'm working on Microsoft Access 2007 and Windows 10, just in case.

Thanks in advance.

英文:

I'm trying to import a txt file into my Microsoft Access database but data is being treated as numeric instead of text.

My source file (named sourcefile.txt) is like this:

00120230200001083300091959212311
00120230200001220500220559212311
00120230200240082700083259212311
00120230200240220600224059212311
00120230205001224100234959212311
00120230300001190700193359212311

Around 80.000 rows of data, no fields or delimitation, all rows same length (32 chars). I want to import that data to a table named T_RAW_SWD with just 2 fields:

  1. swd is the first field and has been set as text with 255 limit characters
  2. id_swd is autonumeric and is the primay key

So the idea is to import all those data rows as text into field swd. I've tried just reading data using a query like this:

SELECT F1 AS swd FROM [Text;FMT=Delimited;HDR=No;Database=C:\Temp;IMEX=1].filesource.txt;

This code loads the data but as number, not text. I thought using IMEX=1 would make the trick but no. Here's the output:

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

Desired output:

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

I've read some sources but could not figure out a solution. Also, I've tagged the question with VBA because tried with different macros based on some sources like these ones:

https://stackoverflow.com/questions/62820045/excel-import-text-file-using-vba-in-access

https://stackoverflow.com/questions/58174825/how-to-read-txt-files-and-import-them-into-access-table

https://stackoverflow.com/questions/32390960/vba-code-import-a-text-file-into-an-access-table-with-condition

https://stackoverflow.com/questions/12137085/changing-connection-string-in-excel-messes-up-column-data-types/12145949#12145949

DoCmd.TransferText method (Access)

ImportExportSpecification object (Access)

The differents codes I've tested import the data also as numeric. I've managed to load data into queries or tables but as numeric always. I'm totally stuck about what to do to read data as text so i don't lose any info.

If I manually import the data with the wizard it works properly, but there are several files and this task will be daily so I'm trying to make it faster with SQL code invoked from VBA o VBA itself.

Also tried first creating manually with the wizard an import routine and call it from DoCom.TransferText using acImportFixed with no success.

Any ideas of what I'm doing wrong here? I got no errors on importing the data trough code, but i need it as text, not numeric.

By the way, I'm working on Microsoft Access 2007 and Windows 10, just in case.

Thanks in advance.

答案1

得分: 2

感谢@Andre以及他在评论中发布的链接,我已经能够构建一个解决方案。

第一步,在这个答案中,我们看到如何定位所有导入-导出规范。您可以使用以下查询代码来执行:

SELECT MSysIMEXSpecs.SpecName, MSysIMEXColumns.*
FROM MSysIMEXColumns INNER JOIN MSysIMEXSpecs 
     ON MSysIMEXColumns.SpecID = MSysIMEXSpecs.SpecID

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

第一列保存了每个规范的名称,以防您需要像我一样通过VBA使用其中一个。

第二步,我手动创建了一个用于导入数据的规范。您需要点击“高级”。

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

请注意,在上面的图像中,我们可以根据需要设置导入数据(我只需要一个名为“swd”的单个字段,字符1-32)。这里棘手的部分是两个名为“另存为”和“规范”的按钮。第二个按钮用于加载保存的规范,但第一个按钮将允许您保存具有自定义名称的自定义规范。

我创建了我的规范,并将其命名为“spec_swd”。一旦保存,您可以重新运行首次发布的SQL查询,以确保规范以分配的名称存在。

完成了所有这些步骤后,只需几行代码,我们就可以正确导入txt文件:

Sub test()
Dim sourceFile As String '要导入的源文件
Dim spec As String '规范名称
Dim destTable As String '目标表

sourceFile = "C:\Temp0623.txt"
spec = "swd_spec"
destTable = "T_RAW_SWD"

Application.DoCmd.TransferText acImportFixed, spec, destTable, sourceFile, False

End Sub

使用DoCmd.TransferText方法(Access),一切都可以正常工作。

英文:

Thanks to @Andre and the links posted in his comment I've been able to build a solution.

First step, in this answer we see how to locate all import-experto specifications. You can do it using a query with this code:

SELECT MSysIMEXSpecs.SpecName, MSysIMEXColumns.*
FROM MSysIMEXColumns INNER JOIN MSysIMEXSpecs 
     ON MSysIMEXColumns.SpecID = MSysIMEXSpecs.SpecID

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

First column hold the names of each specification in case you need to use one trough VBA like myself.

Second step, I've created manually a specification to import data using the wizard. You need to click on Advanced

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

如何将txt文件导入Microsoft Access,并将字段设置为文本,而不是数字。

Notice in the image above we can set up as we need to import data (i just need to 1 single field named swd chars 1-32). The tricky part here is the 2 buttons named Save As and Specifications. The second one is to load a saved specification but the first one will allow you save your custom spec with a custom name

I've created my spec and I've named it spec_swd. Once saved, you can rerun the SQL query posted at first to make sure you spec exists with the assigned name.

Once you've done all this steps with a few lines of code we can import the txt file properly:

Sub test()
Dim sourceFile As String 'source file to import
Dim spec As String ' specification name
Dim destTable As String 'destiny table

sourceFile = "C:\Temp0623.txt"
spec = "swd_spec"
destTable = "T_RAW_SWD"

Application.DoCmd.TransferText acImportFixed, spec, destTable, sourceFile, False

End Sub

Everything works now using DoCmd.TransferText method (Access)

huangapple
  • 本文由 发表于 2023年6月29日 19:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定