473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Read and display specific data from text file into listbox using VB 6

Mas Juliza Alias
Hi,

I am building a program on Information System for Reservoir Operation for my Masters research. Now I have a text file which is an output file from a processing software (Terramodel) that lists out Elevation Range and Volume in columns and rows. The text file looks like this;

-------------------------------------------------
-software's distributor address-
-project description-

Depth range Volume
(m) (m3)
0.000 > 0.001 10.0
0.001 > 0.002 12.0
. .
. .
. .

cummulative volume
(m3)
500
-------------------------------------------------

now i've written the code to display the data in listbox using AddItem, but the whole lines are displayed in one single line in the listbox. I want to display only the Depth Range and Volume data, starting from '0.000 > 0.001'. So, how to skip reading the file header (software's distributor address & project description) and the data column's header (Depth range (m) & Volume (m3))?

Thank You for your time.
Sep 24 '10 #1

✓ answered by Guido Geurs

I hope this will help: see attachment

21 10047
Guido Geurs
767 Expert 512MB
I hope this will help.
(see also attachment)

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim FILEIN As Integer
  3. Dim INPUTTEXT As String
  4. Dim COUNTER As Integer
  5.    FILEIN = FreeFile()
  6.    On Error GoTo Error_Open_File
  7.    Open App.Path & "\test.txt" For Input As #FILEIN
  8.       Do Until EOF(FILEIN)
  9.          Line Input #FILEIN, INPUTTEXT
  10.          If InStr(INPUTTEXT, ">") Then List1.AddItem (INPUTTEXT)
  11.       Loop
  12.    Close #FILEIN
  13. Exit Sub
  14. Error_Open_File:
  15.    Close #FILEIN
  16.    MsgBox "There is an error opening the file"
  17. End Sub
Sep 24 '10 #2
hi ggeu,

tq, that works nicely. but still, there is a problem in that. it reads only the depth range, not the volume (value 10.00, next to 0.000 > 0.001). 1 more thing, how can i separate the values before and after the symbol ">" in different columns? the final display should be in 3 columns, 1) initial depth, before ">", 2) end depth, after symbol ">", and 3) volume. all three columns need to be declared into three variables perhaps in arrays, because it will be used in later process. how can i do this?

your help is most appreciated, TQ.
Sep 27 '10 #3
Guido Geurs
767 Expert 512MB
I hope this will help:
If You want a table with Columns, than You have to use a Grid. (see attachment).
You can use a array to split the inputtext and use "AddItem" to place the different parts from the array in the grid.
Sep 28 '10 #4
it works on your test.txt file.but it failed on my text file. i've attached my file here.i think the problem is the data in this file is not saparated by a fixed space.

thanx for ur help,
Mas Juliza
Attached Files
File Type: txt VOL.txt (71.3 KB, 644 views)
Oct 1 '10 #5
Guido Geurs
767 Expert 512MB
The code "Split" can be used if there is only 1 space between the data.
Here we have to use the "Mid" code but then the data must always be on the same place !!!

Is this the case in all the documents You have to Analise ??

If not, we have to find an other way to isolate the data in the TXT file.

Attached is a program with "Mid" code.
Oct 1 '10 #6
that works great to saparate the data in columns. but that's not a standard case. they are not fixed. here's the code with another data file (attached) that cannot be read correctly.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     Dim InputText As String
  3.     Dim FileNum As Integer
  4.     Dim Depth1 As Double
  5.     Dim Depth2 As Double
  6.     Dim FillVol As Double
  7.     Dim CutVol As Double
  8.     With MSFlexGrid1
  9.     'Set grid
  10.     .FixedCols = 0
  11.     .Cols = 4
  12.     .FixedRows = 1
  13.     .Rows = 1
  14.     .TextMatrix(0, 0) = "Initial Depth"
  15.     .TextMatrix(0, 1) = "End Depth"
  16.     .TextMatrix(0, 2) = "Cut Volume"
  17.     .TextMatrix(0, 3) = "Fill Volume"
  18.     'Fill Grid
  19.     FileNum = FreeFile()
  20.     On Error GoTo Error_Open_File
  21.     Open App.Path & "\cubic.txt" For Input As #FileNum
  22.     Do Until EOF(FileNum)
  23.         Line Input #FileNum, InputText
  24.         InputText = Trim(InputText)
  25.         If InStr(InputText, ">") Then
  26.             MSFlexGrid1.AddItem Mid(InputText, 1, 6) & vbTab & _
  27.             Mid(InputText, 10, 6) & vbTab & Mid(InputText, 32, 4) & _
  28.             vbTab & Mid(InputText, 50, 4)
  29.         End If
  30.     Loop
  31.     Close #FileNum
  32.     End With
  33. Exit Sub
  34. Error_Open_File:
  35.     Close #FileNum
  36.     MsgBox "There is an error opening the file"
  37. End Sub

how can i isolate the data and asign them into variables in arrays? these data will be used in later operation to generate graph, so i need to have them in variables as Double.

your helps are most appreciated.
Attached Files
File Type: txt cubic.txt (2.0 KB, 477 views)
Oct 4 '10 #7
Guido Geurs
767 Expert 512MB
is it always the "Cut volume" OR "Fill Volume" ?
Not the 2 in the same line ??

I have modified the code so it can read any file, if there is a value or not or when the columns are on an other place.
The data is also placed in an array (as double).

How it works:

The data lines of the textfile are read in a textbox.
On the textbox You have to place the column separators by typing the value or clicking on the arrows (left= down, right= up)(default I have set it to 40 and 60 but You can change it if the most of Your documents have an other layout)

The separators must be placed in the spaces between 2 data for each line.

If there is a line in which the separator is in the data, You can modify the line in the textbox by adding or deleting spaces (we use this data from the textbox and not from the file for filling the array and the grid)

click on "Fill Grid" to see the data (and also in the array because it's captured from it)
Oct 4 '10 #8
WOW! you're such an expert! you creatively solve the problem. the code works very well. it seems complicated and takes time for me to understand line by line. however there is 1 problem here, the same values in both TextBox (TextEdit) and MSFlexGrid1 are not the same. for example, value 5.000 in TextEdit becomes 5000 in the grid. i don't know how to edit your code because it is beyond my basic VB knowledge.

tq for your time.
Nov 2 '10 #9
Guido Geurs
767 Expert 512MB
Now You are highly pleased with the concept, we can go on with the tuning-up of the program.

I have attached the next step:
- with some more explanation on each step in the code.
- the cells in the grid are now Strings, any objection?

Still to do:
- We don't want to edit the VB code if we want to open an other Datafile! therefor we have to implement the CommonDialog so the user can browse and select a Datafile.
- The default value of the ColSeparator must be saved.

Do You agree with this? §;o)>
Nov 2 '10 #10
THANKS for the explanation. it REALLY helps my understanding on your code :)

but there's one line i can't understand.
Expand|Select|Wrap|Line Numbers
  1. FrameCol(Index).Left = TextEdit.Left + 45 + (Val(TextCol(Index)) * 120)
why + 45? and why * 120? sorry for my limited knowledge on this.

the values in the grid should be in Double, because they will be used in later calculations. i've tried to change String to Double but they turns out values without the decimal points. how can we fix this?

yes, i agree with the CommonDialog idea. i already have it in previous dialog of downloading the data in this program. i'll edit your code in reading the file from App.Path to the datafile selected by user.

tq, hope to hear from u soon.
Nov 3 '10 #11
Guido Geurs
767 Expert 512MB
Framecol(index) are the red and blue lines over the textbox.
To place them on the right place on the textbox: the .Left dimension =
- TextEdit.Left = left side of the textbox
- + 45 [twips] = 3 pixels for the frame of the textbox
- + (Val(TextCol(Index)) * 120) = the value of spaces we have set * 120 [twips] (= 8 pixels)
Remark: You have to use a font in the textbox with for each char the same width = Fixedsys, ... NOT Arial, ...!
You can experiment with these values and see what happens.
Also use the same font for the ruler !

Ok I will place the values in Double and find a way to make them visible with dot-zeros.
I wll also implement the commondialog and the settings of the default values.
Nov 3 '10 #12
Guido Geurs
767 Expert 512MB
I don't think it's possible to place the values in the grid in a Double format.
The grid has only String formats in the cells.
If You want the values later for calculation, You have to read them in the grid as string and transform them with CDbl(...) in the calculation code.

Attached are the latest modifications
Nov 3 '10 #13
this code is perfect! now i want to read the data in the grid, do some calculation to the data, and display them in another table- in another form, perhaps. are the data of Depth1, Depth2, CutVol, and FillVol in array? let say in the new table there are 2 columns: 1)Depth 2)Volume
here's the whole picture:-

MSFlexGrid1_________________________
Depth1 | Depth2 | CutVol | FillVol |
------------------------------------
-5.00 | -4.00 | 41.0 | |
-4.00 | -3.00 | 51.9 | |
-3.00 | -2.00 | 64.1 | |
-2.00 | -1.00 | 77.5 | |
-1.00 | 0.00 | 92.2 | |
------------------------------------

and the new table should be like this:-

MSFlexGrid2_________________________
Depth | Volume |
-----------------
-5.00 | 0.00 | 'Volume(1) always being 0.00
-4.00 | 41.0 | '=CutVol(1) + Volume(1)
-3.00 | 92.9 | '=CutVol(2) + Volume(2)
-2.00 | 157.0 | '=CutVol(3) + Volume(3)
-1.00 | 234.5 | '=CutVol(4) + Volume(4)
0.00 | 326.7 | '=CutVol(5) + Volume(5)
-----------------

how to read the data from MSFlexGrid1, do some calculation on them, and display the result in MSFlexGrid2?

thanks a lot for your continuous help. i REALLY appreciate that.
Nov 10 '10 #14
Guido Geurs
767 Expert 512MB
I hope this will help ( see attachment)
Attached Files
File Type: zip Read and display specific data_v5.1.zip (40.2 KB, 255 views)
Nov 11 '10 #15
i have tried running the program but the result (Volume) is not accurate. i have attached the data i used and the result should be the same as in my previous post. for this text data (DATUM_0.txt), there should be 6 lines of Depth and Volume filled in the grid (Depth= -5.000, -4.000, -3.000, -2.000, -1.000, 0.000). i tried to edit your code but i fail to get the result as i described above.

thank you for your time...
Attached Files
File Type: txt DATUM_0.txt (1.6 KB, 447 views)
Nov 12 '10 #16
Guido Geurs
767 Expert 512MB
In Your datafile there is no line with 0.000 so it is not taken in consideration by setting up the ARRAYDATA.
Must there always be a last line , not present in the data file?
I suppose it will not always be 0.000 ?
If so You will have to add it in the TextEdit window like in the next example You will have to add O.OO2 with no values?:
...
...
0.000 > 0.001 10.0
0.001 > 0.002 12.0
O.OO2

If so i must change the code because the splitting of the last line will give an error because there are no spaces in the line !!!
Nov 12 '10 #17
Guido Geurs
767 Expert 512MB
Is it in the data input file always a constant value between the depth1's ?
If so, I can integrate it in the code.
Nov 13 '10 #18
the last line of Depth column (in the new form) should be the last value of Depth2. can that be coded?

yes, the values between the Depth1 are always constant within a single file. other files may use other constant values.
Nov 15 '10 #19
Guido Geurs
767 Expert 512MB
I hope this will help: see attachment
Attached Files
File Type: zip Read and display specific data_v5.3.zip (20.3 KB, 286 views)
Nov 15 '10 #20
the calculated Volume at the first 3 rows are correct, but the following rows are incorrect. they appear as 156.1, 233.5, and 325.2 which they should be 157.0, 234.5, and 326.7 respectively. i went through your codes finding the calculation formula but i don't know how to edit those. sorry for my limited knowledge. can you help me on this?

thank you for your time...
Nov 16 '10 #21
MMcCarthy
14,534 Expert Mod 8TB
I have closed this thread to further posting. Once you have received an answer to your first question you should choose the post you feel was most helpful as "Best Answer". Then ask your next question in a new thread.

Having multiple questions in a thread is too confusing both to experts and guests looking for answers to their own questions.

Mary
Nov 16 '10 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
1
by: christine0207 | last post by:
Hi, how to generate a fixed length data text file using Scripting.TextStream / filesystemobject.
2
by: JMG | last post by:
Hi All, I have no idea if this is the correct place to post this question, but I'm hoping so :). My problem is that I have a text file containing 5 sets of passwords on a single line space...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
2
by: JB | last post by:
I'm trying to read a text file, and store the position of a specified line, and then later seek back to that line and start reading again. I'm using simple code like the following: StreaReader...
1
by: Paul Lemelle | last post by:
I am trying to read in a text file then use the data in the file. I know how to read a file wiht the System.IO namespace: FileRead fr = new StreamReader("data.txt");...
3
by: printline | last post by:
Hello All I need a little help with a phph script to display some specific data from a mysql table. I have a mysql table with 4 columns and 10 rows. I want to display fx. data from row 4, 6, 8...
1
by: MiziaQ | last post by:
How can I delete a row of data from a data/text file in vb6 ? I am using an MSFlexGrid, so the user can select which row is to be deleted. Thanks in advance, MiziaQ
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
0
by: rahulsapra | last post by:
hello everyone m new to j2me plzz help I have a text file that contains data line by line.. format is : response:<some text><space>Pin:<some text><space>time:<hh:mm:ss> eg response:invalid...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.