Access 和 VBA 读取每行超过 255 个字符的文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10744113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access and VBA read text file with more than 255 characters per line
提问by ichigo
I am trying to read a text file in Access using VBA. The code is as follows:
我正在尝试使用 VBA 在 Access 中读取文本文件。代码如下:
Open "C:\Test\test.txt" For Input As #1
Dim MyString as String
Dim x as integer
x = 0
For x = 0 to 100
Input #1, MyString
MsgBox MyString
Next x
So the purpose of this code, is to iterate through a text file reading line by line and printing it out. But there is the probability in which the line of text exceeds 255 characters. Is there a way to read lines over 255 characters and store them in VBA? Thank you.
所以这段代码的目的是遍历文本文件,逐行读取并打印出来。但是有可能文本行超过 255 个字符。有没有办法读取超过 255 个字符的行并将它们存储在 VBA 中?谢谢你。
Edit: Text File Example
编辑:文本文件示例
1110; TESTING ; 1111; TESTING2 ; 5; 999990981; 10-30-2011; 12-01-2011; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; 133370001; F; 13371; 1; TEST1 ; 000000000; 133370001; 0; TEST ; TESTTES ; TEST ; 501; 10001; 0; 00001;
1112; TESTING ; 1113; TESTING2 ; 3; 999990982; 10-02-2011; 10-30-2011; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; 133370002; F; 13372; 2; TEST2 ; 000000000; 133370002; 0; TEST1 ; TESTTESTT ; TES ; 502; 10002; 0; 00002;
1113; TESTING ; 1114; TESTING2 ; 21; 999990983; 03-01-2011; 10-02-2011; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; 133370003; F; 13373; 3; TEST3 ; 000000000; 133370003; 0; TTESTTESTT ; TESTTESTTES ; TESTTES ; 503; 10003; 0; 00003;
回答by Dick Kusleika
Sub ReadLines()
Dim sInput As String
Dim i As Long
Open "C:\Users\dick\test.txt" For Input As #1
Do While Not EOF(1)
Input #1, sInput
Debug.Print Len(sInput), sInput
Loop
End Sub
I get
我得到
468 1110; ...
469 1112; ...
469 1113; ...
So I'm not seeing that limitation
所以我没有看到那个限制
回答by ichigo
It seems weird that using Input #
was giving me a hard time. I was not getting an error but instead I was receiving half of some strings. I tried to look at each line of the text to see if there were anomalies but I couldn't find any. In the end, I tried @Matt Donnan approach of using TextStream and that work. Thank you all and sorry for taking your time.
使用Input #
让我很难受,这似乎很奇怪。我没有收到错误,而是收到了一些字符串的一半。我试图查看文本的每一行,看看是否有异常,但我找不到任何异常。最后,我尝试了使用 TextStream 的 @Matt Donnan 方法和这项工作。谢谢大家,很抱歉耽误了您的时间。
回答by user3220335
I believe the problem has to do with the size limit of the MsgBox. Debug.Print will print to the Immediate Window (accessible by Ctrl+G).
我相信问题与 MsgBox 的大小限制有关。Debug.Print 将打印到立即窗口(可通过 Ctrl+G 访问)。