0

I am trying to read a text file in Access using VBA. The code is as follows:

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.

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;    
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
ichigo
  • 317
  • 2
  • 6
  • 17
  • possible duplicate of [How can I read data from a text file using VB6?](http://stackoverflow.com/questions/2873830/how-can-i-read-data-from-a-text-file-using-vb6) – GSerg May 24 '12 at 19:37
  • I can't find any documentation that `Input #` stops at 255 characters. Can you provide a reference? – mellamokb May 24 '12 at 19:42
  • I have added an example file. – ichigo May 24 '12 at 20:29
  • Like the others I haven't found any limitations, but I tend to use the MS Scripting Runtime Library and the FileSystemObject TextStream approach instead. As Remou said, as long as the field is set to memo rather than text then you shouldn't run into problems. – Matt Donnan May 25 '12 at 09:45
  • @ichigo: What happens when you read a line of more than 255 chars? Anything particular? – Jean-François Corbett May 25 '12 at 10:29

3 Answers3

2
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

Dick Kusleika
  • 32,673
  • 4
  • 52
  • 73
0

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.

ichigo
  • 317
  • 2
  • 6
  • 17
0

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).