Reading text files
I am attempting to read a tab delimited text file. What I need to be able to do is as each vbcrlf is reached in my file, a new text box gets created at runtime to accept the next tab delimited line in the text file, if this makes sense.
Any help is greatly appreciated.
Paul Rosebush
# 1 Re: Reading text files
First, care must be taken to prevent too many text boxes from being created. However, what comes to mind is to use Split() on the entire file, to form an array. Then you can use the number of elements to create the textboxes, and set each one to the corresponding element.
# 2 Re: Reading text files
Wizbang,
I greatly appreciate your response however, I would say my knowledge of the split function and arrays is limited. Would you be able to supply a sample of the code required to do this?
Again, greatly appreciated.
Regards,
Paul Rosebush
# 3 Re: Reading text files
Here is an example of one way to do it. Be sure to add error handling, and it is probably a good idea to account for files that may not have a CrLf at the end of every line.
# 4 Re: Reading text files
Are you going to have a textbox for each field multiplied for each line?
Better you change your mind and use a flexgrid, instead, with only one textbox for editing...
# 5 Re: Reading text files
'****************************************
Whooopsss... sorry Wiz, did not saw your answer...
'****************************************
If you switch your mind to flexxgrid, here a sample on how to
load data from a tab delimited file. You can search the forum to
get an example on how to "edit" the content of flexgrid...
# 6 Re: Reading text files
Gentlemen, I am quite in your debt for your asistance. Wizbang, your attachment was of immense help. I am forced to use text boxes for a reason. You mention filling up the screen with text boxes and taking care to maintain that objects don't run off the screen. My file size will of course determine how many text boxes are required. It appears I will have to create pages/panels to accept the incoming data should the file be larger than the screen permits.
The FileToArray attachment does exactly what I need however, I need to format the text. Assume that each tab in a line represents a field, each line in the file represents another record.
Your attachment returns:
XXXXX|YYYYY|ZZZZZ
I need to display in the textbox:
XXXXX
YYYYY
ZZZZZ
Essentially, the prog must convert a tabbed list to sheets of labels.
I realize you Gentlemen probably have much better things to do however, as I mentioned above, I am in your debt. I am a graphics programmer, do not hesitate to query me.
Regards,
Paul Rosebush
# 7 Re: Reading text files
So you want each tab to be a CrLf, right? In that case, you can use Replace() to change all Chr(9) to vbCrLf. You will also need to set each textbox to Multiline.
One thing I might do to prevent too many boxes is to load only one "page" at a time. Let's suppose you have a limit of ten boxes. You can just set a variable which determins which group of ten strings is loaded into the boxes. You only have to create the boxes once, and only ten, or less if the file is small enough. A scrollbar could be used to navigate through the strings. You can still load the whole file if it's small enough too. If it's large, then moving the scrollbar can determin the place in the file to start loading your ten lines.
# 8 Re: Reading text files
I may require a vbcrlf for each tab however each line in the text file should be treated as a single record, the tabs define the fields in the record. Although the text file is created as tabbed single line records, I need to display each record in its own text box but instead of single line (like the text file) I need to display the records/lines as follows:
TEXT FILE VIEW (as opened in notepad)
Pte. John Hancock <tab> 3rd Div. 1st Bat., RCAF <tab> 1941-1945 <tab> D-Day, France, Germany, N.W. Europe
Pte. B. MacDonald <tab> Queen's Own Rifles <tab> 1942-1946 <tab> Normandy Campaign, KIA Dieppe 1946
The above constitutes two (2) lines of the text file. There may be hundreds of lines in a given file (hence the need for pages).
The following is how the data must be displayed within a textbox in my prog:
[Textbox1]
Pte. John Hancock
3rd Div. 1st Bat., RCAF
1941 - 1945
D-Day, France, Germany, N.W. Europe
[Textbox2]
Pte. B. MacDonald
Queen's Own Rifles
1942 - 1946
Normandy Campaign, KIA Dieppe 1946
In short, each line of the text file must be in its own textbox.
Each textbox must have the tab coverted to a vbcrlf, I think, to achieve the above.
Regards
# 9 Re: Reading text files
Yes, according to the information you supplied, I'd use Replace() to make all tabs into CrLf. One issue I forgot to mention is that you will need to set the height of the boxes. If each record will always have the same number of fields, then it makes this easier. Thus far you have not specified whether this data is to be edited and resaved to a file. If not, then you actually won't need textboxes, and labels could be used. Although even if the data was to be editable, I'd use labels anyway, and probably a set of 4 textboxes that would be used to edit the fields. Then you can have more control over how data is entered, thus minimizing the chances of errors. This also helps cut down on resource requirements, as alluded to by Cimperiali.
# 10 Re: Reading text files
Wizbang,
I will be away for approx. one week on business and when I return I will give you the details of exactly what this prog must do so you may better understand.
As usual, all help is greatly appreciated.
Regards,
Paul
# 11 Re: Reading text files
Back from business now, would like to further discuss my prog if you are still interested, Wizbang.
Regards,
Paul Rosebush
# 12 Re: Reading text files
You may also find out that there is a limit of 255 contorls on a page. One of those being the page.
# 13 Re: Reading text files
Back from business now, would like to further discuss my prog if you are still interested, Wizbang.
Regards,
Paul Rosebush
Sure. If you can address my last post, we'll pick up where we left off.
# 14 Re: Reading text files
Wizbang,
The data need only be displayed (non-edit). The displayed data will go to a printer. No saving is required. The printer table is 24"x 22" approx. I'm not sure if there is a better way to do it . I picture a prog that has a page size as above, similar to the page layout of a drawing program, and given the parameters (user supplied label/char size etc.), creates and situates the text labels to maximize the use of the 24x22 printer table. Should the text file size exceed the print table size, another page is created.
The reason for the textbox was purely to create a printable border around the label however, if I'm not mistaken, the line tool is not considered a control but can be printed.
Regarding your last reply, I aggree with the Replace(), though I am confused on the order of things to happen. A single line of the text file must be contained within one printable label. If we Replace() the tabs with crlf's then the last crlf in the line must create the new label, correct me if i am wrong.
Regards,
Paul
# 15 Re: Reading text files
...If we Replace() the tabs with crlf's then the last crlf in the line must create the new label, correct me if i am wrong.
This is not the case since the Replace() function would be used on the individual array elements once the data has been Split. However, it can still be achieved all at once by replacing all CrLf's with some unused character, such as Chr(0). Then replacing all tabs with vbCrLf, and then Split() the data on Chr(0). The downside might be some noticable delay and of course memory usage if the file is large enough.
You can use labels instead of textboxes, as the attached example demonstrates. The border can be a single line or have the same appearence as the default border of a textbox.
All this however may not be what you need to do though. If I understand correctly, the page size is rather large. Since the printer object has an hDC, you could use API calls to place the text where you want, plus you will have a much easier time displaying a print preview. There would be no labels to run off the screen either.
# 16 Re: Reading text files
tried your code and got a runtime error 9
'Subscript out of range' on the following line;
Label1(0) = Replace(S(0), vbTab, vbCrLf)
# 17 Re: Reading text files
my fault, sorry Wizbang, forgot to add the tabs to c:/test.txt.
# 18 Re: Reading text files
Yes, being just an example I didn't attempt to prevent or trap all possible errors. Two things that come to mind are:
1) Opening for Binary will create the file if it does not exist.
2) You can check the UBound of the array to see if it is above -1 before allowing the rest of the code to execute.
(you can edit and delete your own posts)
# 19 Re: Reading text files
This is slightly aside from what you guys are talking about, but if you're using the text file to print labels, would it not be easier to import the text file into something like an Access database using the import tool, and then use the report wizard to create a "label" report?
# 20 Re: Reading text files
The extra overhead for the database would far exceed what is needed for such a simple task.
# 21 Re: Reading text files
Wizbang,
Your last example returned only the first line of the text file, not all lines.
I've been disecting various drawing type progs to try and determine the best method for populating a 24"x22" page size, still at it. I can't help but feel there is a very simple solution to my problem.
I viewed a prog that used a sort of thumbnail object that would, based on user supplied parameters such as width/height of label, populate a page view of a book. It had some of the functionality I am looking for however, contacting the author has proven fruitless.
Another way might be to use the draw method to create the labels as they are strictly for printing.
Still at it,
Paul
# 22 Re: Reading text files
Your last example returned only the first line of the text file, not all lines.
It worked for me. I simply made a file with the two example lines you gave in this thread. Are you sure you had a Cr+Lf at the end of the lines?
The file I made looked like this:
Pte. John Hancock<TAB>3rd Div. 1st Bat., RCAF<TAB>1941 - 1945<TAB>D-Day, France, Germany, N.W. Europe
Pte. B. MacDonald<TAB>Queen's Own Rifles<TAB>1942 - 1946<TAB>Normandy Campaign, KIA Dieppe 1946
# 23 Re: Reading text files
Yes, there are simple solutions. One way is demonstrated in the attached example. The printer object has much of the same capabilities as a picturebox (which is what I used), so it shouldn't take much to modify this for your purpose.